0

我在导航屏幕时遇到了一个小问题,实际上我通过菜单按钮创建了一个事件以显示 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);

    }

}

文件视图.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
4

1 回答 1

1

您正在使用 ListActivity 。所以 ListActivity 期望你的内容视图有一个 id 的 listView@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>
于 2012-09-04T07:13:44.880 回答