ListActivity メニューを実装したいアプリケーションを作成します。このチュートリアルを使用しました。1 つの問題があります。
アプリケーションを実行して 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