list view button error

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

list view button error

Postby SamB09 » Mon Feb 01, 2010 6:33 pm

Hi guys im trying to put a button on each row of a list but i get null pointer exception, i'll show the java files below. The list view is in a seperate xml file to the button because button is inside a relative layout. Can anyone help?.

This class gets data from a database and puts it in array and displays in a list view which works fine until i try to put a button in each row.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package project.Maps;
  3.  
  4. public class SatMain extends Activity {
  5.  
  6.     /** Called when the activity is first created. */
  7.  
  8.         ArrayList<String> results = new ArrayList<String>();
  9.  
  10.         @Override
  11.  
  12.     public void onCreate(Bundle savedInstanceState)
  13.  
  14.     {
  15.  
  16.         super.onCreate(savedInstanceState);
  17.  
  18.         setContentView(R.layout.main);
  19.  
  20.         dbAdapter db = new dbAdapter(this);
  21.  
  22.      
  23.  
  24.                 ;
  25.  
  26.                                        
  27.  
  28.  
  29.  
  30.  
  31.  
  32.         //---get all titles---
  33.  
  34.         db.open();
  35.  
  36.           db.InsertData();
  37.  
  38.        
  39.  
  40.         Cursor c =  db.getSat1();
  41.  
  42.      
  43.  
  44.         if (c.moveToFirst())
  45.  
  46.         {
  47.  
  48.             do {          
  49.  
  50.                  String pub =  c.getString(c.getColumnIndex(db.KEY_Artist));
  51.  
  52.                  String pub1 =  c.getString(c.getColumnIndex(db.KEY_Time));
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.               results.add(pub + pub1 );
  61.  
  62.             } while (c.moveToNext());
  63.  
  64.         }
  65.  
  66.         db.close();
  67.  
  68.        
  69.  
  70.         ListView listProducts;
  71.  
  72.         List<String> results = new ArrayList<String>();
  73.  
  74.         Context cont = null;
  75.  
  76.                 customArray ca = new customArray(cont, R.layout.button, results);
  77.  
  78.         listProducts = (ListView)findViewById(R.id.list123);
  79.  
  80.         listProducts.setAdapter(ca);
  81.  
  82.         ca.notifyDataSetChanged();             
  83.  
  84.     }}
Parsed in 0.036 seconds, using GeSHi 1.0.8.4



Code for my custom adapter.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package project.Maps;
  3.  
  4. import java.util.List;
  5.  
  6. public class customArray extends ArrayAdapter<String> {
  7.  
  8.      int resource;
  9.  
  10.      
  11.  
  12.      public customArray(Context cont, int _resource, List<String> results) {
  13.  
  14.           super (cont, _resource, results);
  15.  
  16.           resource = _resource;
  17.  
  18.      }
  19.  
  20.      
  21.  
  22.      @Override
  23.  
  24.      public View getView(int position, View convertView, ViewGroup parent) {
  25.  
  26.           RelativeLayout rl;
  27.  
  28.          
  29.  
  30.        //   String prod = getItem(position);
  31.  
  32.           if (convertView == null) {
  33.  
  34.                rl = new RelativeLayout(getContext());
  35.  
  36.                LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  37.  
  38.                vi.inflate(resource, rl, true);
  39.  
  40.           } else {
  41.  
  42.                rl = (RelativeLayout)convertView;
  43.  
  44.           }
  45.  
  46.          
  47.  
  48.          
  49.  
  50.           return rl;
  51.  
  52.      }
  53.  
  54. }
  55.  
  56.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
SamB09
Developer
Developer
 
Posts: 39
Joined: Tue Oct 27, 2009 9:22 pm
Location: England

Top

Postby icobasco » Mon Feb 01, 2010 7:41 pm

Can you paste your adapter layout please? :)
User avatar
icobasco
Senior Developer
Senior Developer
 
Posts: 108
Joined: Thu Jun 04, 2009 11:12 am
Location: Italy

Postby SamB09 » Mon Feb 01, 2010 7:48 pm

This is the layout for the button ( i want one of these on each row)


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="UTF-8"?>
  3.  
  4.  
  5.  
  6.         <RelativeLayout
  7.  
  8. android:id="@+id/widget28"
  9.  
  10. android:layout_width="fill_parent"
  11.  
  12. android:layout_height="fill_parent"
  13.  
  14. xmlns:android="http://schemas.android.com/apk/res/android"
  15.  
  16. >
  17.  
  18. <Button
  19.  
  20. android:id="@+id/widget29"
  21.  
  22. android:layout_width="wrap_content"
  23.  
  24. android:layout_height="wrap_content"
  25.  
  26. android:text="Button"
  27.  
  28. android:layout_alignParentTop="true"
  29.  
  30. android:layout_alignParentRight="true"
  31.  
  32. />
  33.  
  34. </RelativeLayout>
  35.  
  36.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


Heres the list view code which is in another file.
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.              android:layout_width="fill_parent"
  6.  
  7.              android:layout_height="fill_parent"
  8.  
  9.              android:orientation="vertical" >
  10.  
  11.    
  12.  
  13.    
  14.  
  15.                         <ListView
  16.  
  17. android:id="@+id/list123"
  18.  
  19.                         android:layout_width="fill_parent"
  20.  
  21.                         android:layout_height="fill_parent"
  22.  
  23.                 android:drawSelectorOnTop="false"
  24.  
  25.                        
  26.  
  27.                        
  28.  
  29.                         />
  30.  
  31. </LinearLayout>
  32.  
  33.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
SamB09
Developer
Developer
 
Posts: 39
Joined: Tue Oct 27, 2009 9:22 pm
Location: England

Postby jwei512 » Tue Feb 02, 2010 7:42 am

Hmm... why do you do this?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Context cont = null;
  3.  
  4. customArray ca = new customArray(cont, R.layout.button, results);
  5.  
  6.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Is there a reason you initialize the context to be null, and pass that in right away?
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Postby SamB09 » Tue Feb 02, 2010 1:31 pm

No reason, it wouldnt allow me just to put "cont" in the constructor which is strange, so i had to initialise it before when it should just go straight into the constructor.
SamB09
Developer
Developer
 
Posts: 39
Joined: Tue Oct 27, 2009 9:22 pm
Location: England

Postby jwei512 » Tue Feb 02, 2010 1:54 pm

But why are you putting "null" as the input? Shouldn't it be getApplicationContext()?
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Top

Postby SamB09 » Tue Feb 02, 2010 3:58 pm

I managed to fix my problem it was stupid , i was selecting the wrong layout :roll: . Have a small problem now though, previously my list view was populated with data from the db now the data isnt there and its just an empty row with a button. Im guessing its something to do with the custom adapter , for example im not giving it my database data maybe im not sure.
SamB09
Developer
Developer
 
Posts: 39
Joined: Tue Oct 27, 2009 9:22 pm
Location: England

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests