我在我想实现 ListActivity 菜单的地方制作应用程序。我使用了本教程。我有一个问题。
如果我运行我的应用程序并运行 ListActivity,模拟器显示错误:
12-27 12:42:34.387: E/AndroidRuntime(756): FATAL EXCEPTION: main
12-27 12:42:34.387: E/AndroidRuntime(756): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tomaszsudol.simplyfun/com.tomaszsudol.simplyfun.PolishWebsites}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
12-27 12:42:34.387: E/AndroidRuntime(756): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
我的代码 PolishWebsites.java:
public class PolishWebsites extends ListActivity {
String[] classes = {"Demotywatory", "Kwejk", "Bebzol" };
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.pl_websites);
setListAdapter(new ArrayAdapter<String>(PolishWebsites.this, android.R.layout.simple_list_item_1, classes));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String cheese = classes[position];
try {
Class myClass = Class.forName("com.tomaszsudol." + cheese);
Intent myIntent = new Intent(PolishWebsites.this, myClass);
startActivity(myIntent);
} catch(ClassNotFoundException e) {
e.printStackTrace();
}
}
}
我的 pl_websites.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/android.R.id.list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
我找到了我犯错的地方。我不必要地添加了这行代码:
setContentView(R.layout.pl_websites);
完成教程练习后,我应该看看脚本是否工作以及如何工作。我添加了上面的行,因为我想在布局中显示 ListView。这是不必要的。
感谢您的回答。
如果我设置
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
Eclipse 显示错误如下。
12-27 12:42:34.387: E/AndroidRuntime(756): FATAL EXCEPTION: main
12-27 12:42:34.387: E/AndroidRuntime(756): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tomaszsudol.simplyfun/com.tomaszsudol.simplyfun.PolishWebsites}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
12-27 12:42:34.387: E/AndroidRuntime(756): at android.app.Instrumentation.callActivityOnCreate