-1

对于许多应用程序,我在模拟器中收到“不幸的是应用程序已停止”错误。所有应用程序都可以具有与我在这里提到的相同的 logcat.java 文件。请发布解决方案。谢谢............

这是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.onitemclicklistener"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.onitemclicklistener.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity 
            android:name="com.example.onitemclicklistener.MainActivity1"
            android:label="@string/app_name" ></activity>
            <activity 
            android:name="com.example.onitemclicklistener.MainActivity2"
            android:label="@string/app_name" ></activity>
                <activity 
            android:name="com.example.onitemclicklistener.MainActivity3"
            android:label="@string/app_name" ></activity>
    </application>

</manifest>

Logcat.java

 05-21 13:45:40.194: E/AndroidRuntime(1380): FATAL EXCEPTION: main
    05-21 13:45:40.194: E/AndroidRuntime(1380): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.onitemclicklistener/com.example.onitemclicklistener.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
    05-21 13:45:40.194: E/AndroidRuntime(1380):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
    05-21 13:45:40.194: E/AndroidRuntime(1380):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
    05-21 13:45:40.194: E/AndroidRuntime(1380):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
    05-21 13:45:40.194: E/AndroidRuntime(1380):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
    05-21 13:45:40.194: E/AndroidRuntime(1380):     at android.os.Handler.dispatchMessage(Handler.java:99)
    05-21 13:45:40.194: E/AndroidRuntime(1380):     at android.os.Looper.loop(Looper.java:137)
    05-21 13:45:40.194: E/AndroidRuntime(1380):     at android.app.ActivityThread.main(ActivityThread.java:4424)
    05-21 13:45:40.194: E/AndroidRuntime(1380):     at java.lang.reflect.Method.invokeNative(Native Method)
    05-21 13:45:40.194: E/AndroidRuntime(1380):     at java.lang.reflect.Method.invoke(Method.java:511)
    05-21 13:45:40.194: E/AndroidRuntime(1380):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    05-21 13:45:40.194: E/AndroidRuntime(1380):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    05-21 13:45:40.194: E/AndroidRuntime(1380):     at dalvik.system.NativeStart.main(Native Method)
    05-21 13:45:40.194: E/AndroidRuntime(1380): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
    05-21 13:45:40.194: E/AndroidRuntime(1380):     at android.app.ListActivity.onContentChanged(ListActivity.java:243)
    05-21 13:45:40.194: E/AndroidRuntime(1380):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:254)
    05-21 13:45:40.194: E/AndroidRuntime(1380):     at android.app.Activity.setContentView(Activity.java:1835)
    05-21 13:45:40.194: E/AndroidRuntime(1380):     at com.example.onitemclicklistener.MainActivity.onCreate(MainActivity.java:22)
    05-21 13:45:40.194: E/AndroidRuntime(1380):     at android.app.Activity.performCreate(Activity.java:4465)
    05-21 13:45:40.194: E/AndroidRuntime(1380):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
    05-21 13:45:40.194: E/AndroidRuntime(1380):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
    05-21 13:45:40.194: E/AndroidRuntime(1380):     ... 11 more
    05-21 13:45:40.554: I/dalvikvm(1380): threadid=3: reacting to signal 3
    05-21 13:45:40.584: I/dalvikvm(1380): Wrote stack traces to '/data/anr/traces.txt'
    05-21 13:45:40.924: I/dalvikvm(1380): threadid=3: reacting to signal 3
    05-21 13:45:41.034: I/dalvikvm(1380): Wrote stack traces to '/data/anr/traces.txt'
    05-21 13:45:56.044: I/Process(1380): Sending signal. PID: 1380 SIG: 9

MainActivity.java

 package com.example.onitemclicklistener;

    import android.os.Bundle;
    import android.app.Activity;
    import android.app.ListActivity;
    import android.content.Context;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;

    public class MainActivity extends ListActivity {

        String [] ipl=new String[]{"chennai","mumbai","rajasthan"};
        ListView lv;
        final Context con=this;
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            lv=(ListView)findViewById(R.id.list);
            lv=getListView();
            lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ipl));

            lv.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> Parent, View v, int Position,
                        long id) {
                    Intent i=null;
                    switch(Position)
                    {
                    case 0:
                          i=new Intent(getApplicationContext(),MainActivity1.class);
                          startActivity(i);
                          break;
                    case 1:
                          i=new Intent(getApplicationContext(),MainActivity2.class);
                          startActivity(i);
                          break;          
                    case 2:
                          i=new Intent(getApplicationContext(),MainActivity3.class);
                          startActivity(i);
                          break;


            };

        }

            });
            }
    }
4

3 回答 3

3
Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

您的R.layout.activity_main资源不包含ListViewandroid:id属性为 的android.R.id.list。这是 的要求ListActivity

于 2013-05-21T08:29:47.870 回答
0

为什么需要语句 lv=getListView(); 语句 findViewbyId 将列表视图从 xml 分配给您的列表视图。确保在你的 activity_main 中声明了一个 Listview,id:"@+id/list"。

于 2013-05-21T08:34:58.357 回答
0

您正在使用 listactivity 并且错误说您的活动中没有 id 为 r.id.list 的 listview

于 2013-05-21T08:31:00.590 回答