When I attempt to run this particular activity in my Android app, I get a NullPointerException at the line (appears as a single line in Eclipse) which I've marked with an arrow.
This activity uses a linear layout, and features a ListView in one of the linear, I guess, 'segments'. I'm using the same ListView code in other activities that only have a ListView in the activity, so those classes extend ListActivity.
In the code shown below, I'm just extending Activity as usual, since if I extend ListActivity here, I get an error stating that I must have a ListView whose attribute is 'android.R.id.list', which as you 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 the problem here? The other ListViews access the string array just fine, so how is this 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 to start activity ComponentInfo{com.example.event_booker/com.example.event_booker.BookEvent}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-05 21:30:00.282: E/AndroidRuntime(12791): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
04-05 21:30:00.282: E/AndroidRuntime(12791): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
04-05 21:30:00.282: E/AndroidRuntime(12791): at android.app.ActivityThread.access$1500(ActivityThread.java:121)
04-05 21:30:00.282: E/AndroidRuntime(12791): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
04-05 21:30:00.282: E/AndroidRuntime(12791): at android.os.Handler.dispatchMessage(Handler.java:99)
04-05 21:30:00.282: E/AndroidRuntime(12791): at android.os.Looper.loop(Looper.java:130)
04-05 21:30:00.282: E/AndroidRuntime(12791): at android.app.ActivityThread.main(ActivityThread.java:3701)
04-05 21:30:00.282: E/AndroidRuntime(12791): at java.lang.reflect.Method.invokeNative(Native Method)
04-05 21:30:00.282: E/AndroidRuntime(12791): at java.lang.reflect.Method.invoke(Method.java:507)
04-05 21:30:00.282: E/AndroidRuntime(12791): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
04-05 21:30:00.282: E/AndroidRuntime(12791): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
04-05 21:30:00.282: E/AndroidRuntime(12791): at dalvik.system.NativeStart.main(Native Method)
04-05 21:30:00.282: E/AndroidRuntime(12791): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-05 21:30:00.282: E/AndroidRuntime(12791): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
04-05 21:30:00.282: E/AndroidRuntime(12791): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:230)
04-05 21:30:00.282: E/AndroidRuntime(12791): at android.app.Activity.setContentView(Activity.java:1657)
04-05 21:30:00.282: E/AndroidRuntime(12791): at com.example.event_booker.BookEvent.onCreate(BookEvent.java:22)
04-05 21:30:00.282: E/AndroidRuntime(12791): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-05 21:30:00.282: E/AndroidRuntime(12791): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
04-05 21:30:00.282: E/AndroidRuntime(12791): ... 11 more
The problem is that android.R.id.list refers to the default listview id which is "@android:id/list"
. You can choose to have it in your xml or to have your custom id to refer from the code.
You can either have your ListView have an id such as
android:id="@android:id/list"
Something like
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
or have your 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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.