0

当我尝试在我的 Android 应用程序中运行此特定活动时,我在已用箭头标记的行(在 Eclipse 中显示为单行)处收到 NullPointerException。

此活动使用线性布局,并在其中一个线性“段”中提供 ListView。我在其他只有一个 ListView 的活动中使用相同的 ListView 代码,因此这些类扩展了 ListActivity。

在下面显示的代码中,我只是像往常一样扩展 Activity,因为如果我在这里扩展 ListActivity,我会收到一条错误消息,指出我必须有一个属性为 'android.R.id.list' 的 ListView,你可以看,有。

@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) 
        {

        }

    });
}

这里有什么问题?其他 ListView 可以很好地访问字符串数组,那么这有什么不同呢?

编辑:添加堆栈跟踪

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
4

1 回答 1

0

问题是 android.R.id.list 指的是默认的列表视图 ID,即"@android:id/list". 您可以选择将它放在您的 xml 中,或者让您的自定义 id 从代码中引用。

您可以让您的 ListView 有一个 id,例如

android:id="@android:id/list"

就像是

<ListView 
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> 

或者有你的身份证:

<ListView 
android:id="@+id/yourlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> 

并致电:

final ListView lv = (ListView) findViewById(R.id.yourlist);
于 2013-04-05T21:26:29.620 回答