Simple List Problem

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Simple List Problem

Postby williamzanelli » Thu Apr 02, 2009 11:39 pm

Hey guys,

I've been trying to contsruct a list, I'm having problems. I get a Null Pointe Exception

My code is below, I have a TabHost, from which I call new activity (via Intent), that will populate my Tabcontent area, see below

Code: Select all
public class TabPage extends TabActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabs);
.....
.....
                 host.addTab(host.newTabSpec("one") 
                         .setIndicator("One") 
                         .setContent(new Intent(this, DataList.class)));


The DataList.class is below

Code: Select all
public class DataList extends Activity  {
   public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
       
      String[] dataArray = new String[] {"111111","211111","3111111"}; 
      int [] to = new int[] {R.id.newsItem};   
      ListView lv = (ListView) findViewById(R.id.list_x);
      ArrayAdapter arrayAdapter = new ArrayAdapter (this, R.layout.tab_row, R.id.listItem, dataArray);
     lv.setAdapter(arrayAdapter);


The XML is as below

This is tab_row.xml file,
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/listItem" xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>




Below is the tabs.xml file,
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
               
           
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
         
<ListView
          android:id = "@+id/listn_x"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          />
     </FrameLayout>

</LinearLayout>
</TabHost>



Any idea on what I,m doing wrong?

Thanks in advance for your thoughts
Will
williamzanelli
Experienced Developer
Experienced Developer
 
Posts: 63
Joined: Mon Jan 05, 2009 2:37 am
Location: UK

Top

Postby Yinten » Thu Apr 02, 2009 11:58 pm

Quick stab, you would need to setContentView() beofre you can findViewById(R.id.list_x).


public class DataList extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

String[] dataArray = new String[] {"111111","211111","3111111"};
int [] to = new int[] {R.id.newsItem};
ListView lv = (ListView) findViewById(R.id.list_x);
ArrayAdapter arrayAdapter = new ArrayAdapter (this, R.layout.tab_row, R.id.listItem, dataArray);
lv.setAdapter(arrayAdapter);
Yinten
Developer
Developer
 
Posts: 27
Joined: Tue Jan 20, 2009 7:27 pm

Postby williamzanelli » Fri Apr 03, 2009 12:42 am

Thanks for the reply YinTen

I've chnaged the code as follow

Code: Select all

public class DataList extends Activity  {
   public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
       
       
      setContentView(R.layout.tabs);
       
      String[] dataArray = new String[] {"111111","211111","3111111"};
      ListView lv = (ListView) findViewById(R.id.listnews_x);//ListView is inside tabs.xML file
      ArrayAdapter <String> arrayAdapetr =  new ArrayAdapter<String> (this, R.id.listItem , dataArray);//listItem is in seperate XML file called tab_row.xml
     
      lv.setAdapter(arrayAdapetr);



I get an error even if I add setContentView(R.layout.tabs);

The error says R.id.listItem is not valid.
If I remove, setContentView(R.layout.tabs);


What has to be remembered is that this Activity is being called from a TabActivity class to fill the content.

See this code

Code: Select all
host.addTab(host.newTabSpec("one")
                         .setIndicator("One")
                         .setContent(new Intent(this, DataList.class)));


I dont want the view to chnage, I jsut want it to gather data and populate a list.. i.e. the content area of that tab

Is this the right way to go about such a task?

Thanks for your thoughts
Will
williamzanelli
Experienced Developer
Experienced Developer
 
Posts: 63
Joined: Mon Jan 05, 2009 2:37 am
Location: UK

Postby Yinten » Fri Apr 03, 2009 5:04 am

add this to a new xml file name it test.xml:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "@+id/listn_x"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

Remove the ListView from the xml file tabs.xml.

add this for your DataList:

public class DataList extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);


setContentView(R.layout.test);

String[] dataArray = new String[] {"111111","211111","3111111"};
ListView lv = (ListView) findViewById(R.id.listnews_x);//ListView is inside tabs.xML file
ArrayAdapter <String> arrayAdapetr = new ArrayAdapter<String> (this, R.id.listItem , dataArray);//listItem is in seperate XML file called tab_row.xml

lv.setAdapter(arrayAdapetr);

Your tab host is just the activity of the tabs at the top, it is another activity and layout inside of each of the tabhosts your starting when they are selected underneath.


Hopefully that helps,

Yinten
Yinten
Developer
Developer
 
Posts: 27
Joined: Tue Jan 20, 2009 7:27 pm

Postby williamzanelli » Sat Apr 04, 2009 12:51 am

Right, I've nailed this F****r. 8)

My problem was in my

The code here should have been
Code: Select all
<ListView android:id="@android:id/list"
instead of
Code: Select all
<ListView android:id = "@+id/listnews_x"


My code is as below for anyone else who's having problems

I have a tabs.xml file (for tabs)

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
               
           
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
     </FrameLayout>
</LinearLayout>
</TabHost>


List.xml
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
    android:layout_height="wrap_content">
   
<ListView android:id="@android:id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
  <TextView android:id="@android:id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/no_notes"/>
</LinearLayout>


And row.xml as
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/item" xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>


My code from the TabActivty file, calls a ListActivity class via Intent

Code: Select all
//Host is TabHost class
  host.addTab(host.newTabSpec("one") 
                         .setIndicator("One") 
                         .setContent(new Intent(this,  DataList.class)));


DataList.class is as follows

Code: Select all
public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
       
       
     setContentView(R.layout.List);
         
     String[] dataArray = new String[] {"111111","211111","3111111"};
   ArrayAdapter<String> notes = new ArrayAdapter<String> (this,R.layout.row, dataArray );
   setListAdapter(notes);



And this genteleman works fabulously, thanks for your thoughts in the previous posts :D :D :D
Will
williamzanelli
Experienced Developer
Experienced Developer
 
Posts: 63
Joined: Mon Jan 05, 2009 2:37 am
Location: UK

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 7 guests