1

我一直在使用 Google 的教程编写 ListView,但由于某种原因,错误不断发生。我已经确保创建外部存储权限,就像我在另一篇文章中看到的那样,但仍然发生了一些事情。

主要活动:

public class FlashcardsActivity extends ListActivity {
private ListView mainList;
private final String[] listOptions = {"Test!","New Set", "Edit Existing"};
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_row,listOptions));

mainList=(ListView)findViewById(R.id.mainList);
mainList.setTextFilterEnabled(true);

mainList.setOnItemClickListener(new OnItemClickListener(){
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(getApplicationContext(), ((TextView)view).getText(),Toast.LENGTH_SHORT).show();
    }
});
}
}

日志猫:

06-09 21:29:32.239: D/AndroidRuntime(221): Shutting down VM
06-09 21:29:32.239: W/dalvikvm(221): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
06-09 21:29:32.250: E/AndroidRuntime(221): Uncaught handler: thread main exiting due to uncaught exception
06-09 21:29:32.259: E/AndroidRuntime(221): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aft.flashcards/com.aft.flashcards.FlashcardsActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
06-09 21:29:32.259: E/AndroidRuntime(221):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
06-09 21:29:32.259: E/AndroidRuntime(221):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
06-09 21:29:32.259: E/AndroidRuntime(221):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
06-09 21:29:32.259: E/AndroidRuntime(221):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
06-09 21:29:32.259: E/AndroidRuntime(221):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-09 21:29:32.259: E/AndroidRuntime(221):  at android.os.Looper.loop(Looper.java:123)
06-09 21:29:32.259: E/AndroidRuntime(221):  at android.app.ActivityThread.main(ActivityThread.java:4363)
06-09 21:29:32.259: E/AndroidRuntime(221):  at java.lang.reflect.Method.invokeNative(Native Method)
06-09 21:29:32.259: E/AndroidRuntime(221):  at java.lang.reflect.Method.invoke(Method.java:521)
06-09 21:29:32.259: E/AndroidRuntime(221):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-09 21:29:32.259: E/AndroidRuntime(221):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-09 21:29:32.259: E/AndroidRuntime(221):  at dalvik.system.NativeStart.main(Native Method)
06-09 21:29:32.259: E/AndroidRuntime(221): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
06-09 21:29:32.259: E/AndroidRuntime(221):  at android.app.ListActivity.onContentChanged(ListActivity.java:236)
06-09 21:29:32.259: E/AndroidRuntime(221):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:201)
06-09 21:29:32.259: E/AndroidRuntime(221):  at android.app.Activity.setContentView(Activity.java:1622)
06-09 21:29:32.259: E/AndroidRuntime(221):  at com.aft.flashcards.FlashcardsActivity.onCreate(FlashcardsActivity.java:19)
06-09 21:29:32.259: E/AndroidRuntime(221):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-09 21:29:32.259: E/AndroidRuntime(221):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
06-09 21:29:32.259: E/AndroidRuntime(221):  ... 11 more
06-09 21:29:32.290: I/dalvikvm(221): threadid=7: reacting to signal 3
06-09 21:29:32.290: E/dalvikvm(221): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

主.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

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

</LinearLayout>

有谁知道问题是什么?

4

1 回答 1

1

错误日志很清楚:

06-09 21:29:32.259: E/AndroidRuntime(221): Caused by: java.lang.RuntimeException: 您的内容必须有一个 ID 属性为“android.R.id.list”的 ListView

你的主布局中需要一个ListViewwith android:id="@android:id/list"

于 2012-06-09T21:57:42.680 回答