导航屏幕时遇到一个小问题,实际上我从菜单按钮发出一个事件来显示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
答案 0 :(得分:1)
您正在使用ListActivity。因此,ListActivity希望您的内容View具有标识为@android:id/list
所以将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"
/>
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</LinearLayout>