java - NullPointerException in ListView inside a LinearLayout -
when attempt run particular activity in android app, nullpointerexception @ line (appears single line in eclipse) i've marked arrow.
this activity uses linear layout, , features listview in 1 of linear, guess, 'segments'. i'm using same listview code in other activities have listview in activity, classes extend listactivity.
in code shown below, i'm extending activity usual, since if extend listactivity here, error stating must have listview attribute 'android.r.id.list', can see, there is.
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_book_event); final listview lv = (listview) findviewbyid(android.r.id.list); lv.setadapter(new arrayadapter<string>(this, android.r.layout.simple_list_item_1, getresources().getstringarray(r.array.cities))); <--- final spinner s = (spinner) findviewbyid(r.id.spinner); arrayadapter<charsequence> aa = new arrayadapter<charsequence>(this, r.array.noseats, android.r.layout.simple_spinner_item); aa.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); s.setadapter(aa); s.setonitemselectedlistener(new onitemselectedlistener() { @override public void onitemselected(adapterview<?> arg0, view arg1, int arg2, long arg3) { noofseats = s.getselecteditemposition(); noofseats = noofseats + 1; } @override public void onnothingselected(adapterview<?> arg0) { } }); } what's problem here? other listviews access string array fine, how different?
edit: added stack trace
04-05 21:30:00.282: e/androidruntime(12791): fatal exception: main 04-05 21:30:00.282: e/androidruntime(12791): java.lang.runtimeexception: unable start activity componentinfo{com.example.event_booker/com.example.event_booker.bookevent}: java.lang.runtimeexception: content must have listview id attribute 'android.r.id.list' 04-05 21:30:00.282: e/androidruntime(12791): @ android.app.activitythread.performlaunchactivity(activitythread.java:1659) 04-05 21:30:00.282: e/androidruntime(12791): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1675) 04-05 21:30:00.282: e/androidruntime(12791): @ android.app.activitythread.access$1500(activitythread.java:121) 04-05 21:30:00.282: e/androidruntime(12791): @ android.app.activitythread$h.handlemessage(activitythread.java:943) 04-05 21:30:00.282: e/androidruntime(12791): @ android.os.handler.dispatchmessage(handler.java:99) 04-05 21:30:00.282: e/androidruntime(12791): @ android.os.looper.loop(looper.java:130) 04-05 21:30:00.282: e/androidruntime(12791): @ android.app.activitythread.main(activitythread.java:3701) 04-05 21:30:00.282: e/androidruntime(12791): @ java.lang.reflect.method.invokenative(native method) 04-05 21:30:00.282: e/androidruntime(12791): @ java.lang.reflect.method.invoke(method.java:507) 04-05 21:30:00.282: e/androidruntime(12791): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:866) 04-05 21:30:00.282: e/androidruntime(12791): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:624) 04-05 21:30:00.282: e/androidruntime(12791): @ dalvik.system.nativestart.main(native method) 04-05 21:30:00.282: e/androidruntime(12791): caused by: java.lang.runtimeexception: content must have listview id attribute 'android.r.id.list' 04-05 21:30:00.282: e/androidruntime(12791): @ android.app.listactivity.oncontentchanged(listactivity.java:243) 04-05 21:30:00.282: e/androidruntime(12791): @ com.android.internal.policy.impl.phonewindow.setcontentview(phonewindow.java:230) 04-05 21:30:00.282: e/androidruntime(12791): @ android.app.activity.setcontentview(activity.java:1657) 04-05 21:30:00.282: e/androidruntime(12791): @ com.example.event_booker.bookevent.oncreate(bookevent.java:22) 04-05 21:30:00.282: e/androidruntime(12791): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047) 04-05 21:30:00.282: e/androidruntime(12791): @ android.app.activitythread.performlaunchactivity(activitythread.java:1623) 04-05 21:30:00.282: e/androidruntime(12791): ... 11 more
the problem android.r.id.list refers default listview id "@android:id/list". can choose have in xml or have custom id refer code.
you can either have listview have id such as
android:id="@android:id/list" something
<listview android:id="@android:id/list" android:layout_width="wrap_content" android:layout_height="wrap_content" /> or have id :
<listview android:id="@+id/yourlist" android:layout_width="wrap_content" android:layout_height="wrap_content" /> and call:
final listview lv = (listview) findviewbyid(r.id.yourlist);
Comments
Post a Comment