我的Android应用程序在添加多个活动后停止工作。当我只处理一项活动即主要活动时,它工作正常。在我添加其他活动并尝试将其与列表视图链接后,它会导致问题。意图可能存在问题。
我还有3个名为“PActivity”,“AActivity”和“DActivity”的活动,我想根据列表视图中选择的项目显示这些活动。除了由eclipse添加的默认OnCreate()函数外,所有这三个活动都没有添加代码。
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
String[] options = res.getStringArray(R.array.HomeOptions);
setListAdapter(new ArrayAdapter < String > (this, android.R.layout.simple_list_item_1, options));
getListView().setTextFilterEnabled(true);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
switch (position)
{
case 1:
Intent PIntent = new Intent(this, PActivity.class);
startActivity(PIntent);
break;
case 2:
Intent AIntent = new Intent(this, AActivity.class);
startActivity(AIntent);
break;
case 3:
Intent DIntent = new Intent(this, DActivity.class);
startActivity(DisallowIntent);
break;
default:
break;
}
}
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#800000"
android:text="@string/Home"
android:textColor="#FFFFFF"
android:gravity="center" />
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/HomeOptions" />
</LinearLayout>
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.MyApplication"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.MyApplication.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.MyApplication.PActivity"
android:label="@string/title_activity_p" >
</activity>
<activity
android:name="com.MyApplication.AActivity"
android:label="@string/title_activity_a" >
</activity>
<activity
android:name="com.MyApplication.DActivity"
android:label="@string/title_activity_d" >
</activity>
</application>
</manifest>
logcat的
02-01 15:20:34.280: E/AndroidRuntime(1985): FATAL EXCEPTION: main
02-01 15:20:34.280: E/AndroidRuntime(1985): Process: com.myapplication, PID: 1985
02-01 15:20:34.280: E/AndroidRuntime(1985): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapplication/com.myapplication.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
02-01 15:20:34.280: E/AndroidRuntime(1985): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
02-01 15:20:34.280: E/AndroidRuntime(1985): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
02-01 15:20:34.280: E/AndroidRuntime(1985): at android.app.ActivityThread.access$700(ActivityThread.java:135)
02-01 15:20:34.280: E/AndroidRuntime(1985): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
02-01 15:20:34.280: E/AndroidRuntime(1985): at android.os.Handler.dispatchMessage(Handler.java:102)
02-01 15:20:34.280: E/AndroidRuntime(1985): at android.os.Looper.loop(Looper.java:137)
02-01 15:20:34.280: E/AndroidRuntime(1985): at android.app.ActivityThread.main(ActivityThread.java:4998)
02-01 15:20:34.280: E/AndroidRuntime(1985): at java.lang.reflect.Method.invokeNative(Native Method)
02-01 15:20:34.280: E/AndroidRuntime(1985): at java.lang.reflect.Method.invoke(Method.java:515)
02-01 15:20:34.280: E/AndroidRuntime(1985): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-01 15:20:34.280: E/AndroidRuntime(1985): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-01 15:20:34.280: E/AndroidRuntime(1985): at dalvik.system.NativeStart.main(Native Method)
02-01 15:20:34.280: E/AndroidRuntime(1985): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
02-01 15:20:34.280: E/AndroidRuntime(1985): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
02-01 15:20:34.280: E/AndroidRuntime(1985): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:293)
02-01 15:20:34.280: E/AndroidRuntime(1985): at android.app.Activity.setContentView(Activity.java:1928)
02-01 15:20:34.280: E/AndroidRuntime(1985): at com.myapplication.MainActivity.onCreate(MainActivity.java:22)
02-01 15:20:34.280: E/AndroidRuntime(1985): at android.app.Activity.performCreate(Activity.java:5243)
02-01 15:20:34.280: E/AndroidRuntime(1985): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-01 15:20:34.280: E/AndroidRuntime(1985): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
02-01 15:20:34.280: E/AndroidRuntime(1985): ... 11 more
差异:
在更改ListView的ID之前,我的Android控制台的最后两行是:
[2014-02-02 14:25:50 - MyApplication] Starting activity com.mobilesecurity.MainActivity on device emulator-5554
[2014-02-02 14:25:54 - MyApplication ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.myapplication/.MainActivity }
但是在更改最后一条消息(使用ActivityManager)之后没有显示。我的应用程序开始崩溃后,我开始收到此消息。可能会有所帮助。
答案 0 :(得分:0)
你有
Intent PIntent = new Intent(this, PActivity.class);
startActivity(PIntent);
在onListItemClick(ListView l, View v, int position, long id)
此可能被视为itemclick侦听器,您必须在其中传递上下文
尝试将其更改为:
Intent PIntent = new Intent(MainActivity.this, PActivity.class);
startActivity(PIntent);
并对其他其他代码段执行相同的操作。
修改强>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/HomeOptions" />
将ID更改为:
android:id="@android:id/list"