views:

64

answers:

1

Hi all,

I'm trying to have my listView as defined in my main.xml display a list of my objects. This worked fine when my activity extended on ListActivity and used getListView(). Now I extend from a normal Activity though and want to plug my custom adapter into my R.id.list like this:

   ListView lv = (ListView) findViewById(R.id.list);
   lv.setTextFilterEnabled(true);
   lv.setAdapter(new MyCustomAdapter(this, R.layout.custom_list, nameResults));

where custom_list is the xml file defining a row. My debugger goes bonkers as soon as it hits the setAdapter part, heading into strange files and finally returning a NullPointerException.

My custom adapter btw:

 public class MyCustomAdapter extends ArrayAdapter<String> {
  private String[] nameResults;
  public MyCustomAdapter(Context context, int textViewResourceId, String[] names) {
   super(context, textViewResourceId, names);
   nameResults = names;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   LayoutInflater inflater=getLayoutInflater();
   View row=inflater.inflate(R.layout.custom_list, parent, false);
   TextView label=(TextView)row.findViewById(R.id.Name);
   label.setText(nameResults[position]);
   TextView descr=(TextView)row.findViewById(R.id.Description);
   descr.setText(linkedResults.get(nameResults[position]));

   return row;
  }
 }

Any way I can get it back working? I only changed it so I could get some buttons above it for a previous/next page structure, aka I need to setContentView(R.layout.main); (right? :P . I used this link to build the listview: Custom listview with adapter.

Stacktrace:

07-03 12:18:02.936: ERROR/AndroidRuntime(2747): Uncaught handler: thread main exiting due to uncaught exception
07-03 12:18:03.026: ERROR/AndroidRuntime(2747): java.lang.NullPointerException
07-03 12:18:03.026: ERROR/AndroidRuntime(2747):     at com.rvthof.ShowPage.buildPage(ShowPage.java:102)
07-03 12:18:03.026: ERROR/AndroidRuntime(2747):     at com.rvthof.ShowPage.access$0(ShowPage.java:78)
07-03 12:18:03.026: ERROR/AndroidRuntime(2747):     at com.rvthof.ShowPage$1.run(ShowPage.java:74)
07-03 12:18:03.026: ERROR/AndroidRuntime(2747):     at android.os.Handler.handleCallback(Handler.java:587)
07-03 12:18:03.026: ERROR/AndroidRuntime(2747):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-03 12:18:03.026: ERROR/AndroidRuntime(2747):     at android.os.Looper.loop(Looper.java:123)
07-03 12:18:03.026: ERROR/AndroidRuntime(2747):     at android.app.ActivityThread.main(ActivityThread.java:4363)
07-03 12:18:03.026: ERROR/AndroidRuntime(2747):     at java.lang.reflect.Method.invokeNative(Native Method)
07-03 12:18:03.026: ERROR/AndroidRuntime(2747):     at java.lang.reflect.Method.invoke(Method.java:521)
07-03 12:18:03.026: ERROR/AndroidRuntime(2747):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-03 12:18:03.026: ERROR/AndroidRuntime(2747):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-03 12:18:03.026: ERROR/AndroidRuntime(2747):     at 

dalvik.system.NativeStart.main(Native Method)

A: 

Your custom adapter doesn't appear to instantiate linkedResults anywhere. Did you just crop that out of what you pasted? If not, that could be a problem.

Keith Twombley
Good find but no it has that.
Rvthof