画面をナビゲートしているときに小さな問題があります。実際には、メニューボタンからイベントを作成してSDカードのファイルの内容を表示します。メニューをクリックするたびに実行時例外が発生し、コード、エラーログ、およびxmlをここに添付しました。どんな助けでも大歓迎です。
メニューボタンの取り扱い//メニューボタンの取り扱い
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
startActivity(new Intent(ContactListActivity1.this,App2Activity.class));
finish();
}
return true;
}
App2Activity.java
public class App2Activity extends ContactListActivity1 {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.file_view);
File sdcard= Environment.getExternalStorageDirectory();
File dir = new File(sdcard.getAbsolutePath() + "/dir1/dir2");
File file = new File(dir, "my_group.txt");
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
}
catch (IOException e) {
e.printStackTrace();
}
TextView tv = (TextView)findViewById(R.id.text_view);
tv.setText(text);
}
}
file_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/text_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
エラーログ
09-04 13:51:33.680: W/CursorWrapperInner(25536): Cursor finalized without prior close()
09-04 13:51:33.880: I/System.out(25536): Am I Coming here !!
09-04 13:51:33.900: D/AndroidRuntime(25536): Shutting down VM
09-04 13:51:33.900: W/dalvikvm(25536): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
09-04 13:51:33.940: E/AndroidRuntime(25536): FATAL EXCEPTION: main
09-04 13:51:33.940: E/AndroidRuntime(25536): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.contactlistactivity1/com.example.contactlistactivity1.App2Activity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
09-04 13:51:33.940: E/AndroidRuntime(25536): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
09-04 13:51:33.940: E/AndroidRuntime(25536): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
09-04 13:51:33.940: E/AndroidRuntime(25536): at android.app.ActivityThread.access$600(ActivityThread.java:122)
09-04 13:51:33.940: E/AndroidRuntime(25536): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
09-04 13:51:33.940: E/AndroidRuntime(25536): at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 13:51:33.940: E/AndroidRuntime(25536): at android.os.Looper.loop(Looper.java:137)
09-04 13:51:33.940: E/AndroidRuntime(25536): at android.app.ActivityThread.main(ActivityThread.java:4340)
09-04 13:51:33.940: E/AndroidRuntime(25536): at java.lang.reflect.Method.invokeNative(Native Method)
09-04 13:51:33.940: E/AndroidRuntime(25536): at java.lang.reflect.Method.invoke(Method.java:511)
09-04 13:51:33.940: E/AndroidRuntime(25536): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-04 13:51:33.940: E/AndroidRuntime(25536): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-04 13:51:33.940: E/AndroidRuntime(25536): at dalvik.system.NativeStart.main(Native Method)
09-04 13:51:33.940: E/AndroidRuntime(25536): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
09-04 13:51:33.940: E/AndroidRuntime(25536): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
09-04 13:51:33.940: E/AndroidRuntime(25536): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:254)
09-04 13:51:33.940: E/AndroidRuntime(25536): at android.app.Activity.setContentView(Activity.java:1835)
09-04 13:51:33.940: E/AndroidRuntime(25536): at com.example.contactlistactivity1.App2Activity.onCreate(App2Activity.java:20)
09-04 13:51:33.940: E/AndroidRuntime(25536): at android.app.Activity.performCreate(Activity.java:4465)
09-04 13:51:33.940: E/AndroidRuntime(25536): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-04 13:51:33.940: E/AndroidRuntime(25536): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
09-04 13:51:33.940: E/AndroidRuntime(25536): ... 11 more