我正在尝试从上下文菜单中启动用户选择中的另一个活动。代码上下文菜单的代码是:
case R.id.organize:
Log.d(LOGTAG, "Creating intent");
Intent editIntent = new Intent(getApplicationContext(), AddToList.class);
editIntent.putParcelableArrayListExtra("userPackageList", userPackages);
Log.d(LOGTAG, "Starting activity");
startActivity(editIntent);
return true;
我的AndroidManifest.xml也有两个活动的条目:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.firstapp.MyPackage"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ActivityOne"
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=".ActivityTwo"
android:label="@string/app_name">
</activity>
</application>
</manifest>
输出在switch case中打印两个日志语句,然后崩溃,但出现以下异常:
07-18 12:32:29.516: ERROR/AndroidRuntime(601): FATAL EXCEPTION: main
07-18 12:32:29.516: ERROR/AndroidRuntime(601): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.firstapp.MyPackage/com.firstapp.MyPackage.ActivityTwo}: java.lang.NullPointerException
07-18 12:32:29.516: ERROR/AndroidRuntime(601): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
07-18 12:32:29.516: ERROR/AndroidRuntime(601): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-18 12:32:29.516: ERROR/AndroidRuntime(601): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-18 12:32:29.516: ERROR/AndroidRuntime(601): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-18 12:32:29.516: ERROR/AndroidRuntime(601): at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 12:32:29.516: ERROR/AndroidRuntime(601): at android.os.Looper.loop(Looper.java:123)
07-18 12:32:29.516: ERROR/AndroidRuntime(601): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-18 12:32:29.516: ERROR/AndroidRuntime(601): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 12:32:29.516: ERROR/AndroidRuntime(601): at java.lang.reflect.Method.invoke(Method.java:521)
07-18 12:32:29.516: ERROR/AndroidRuntime(601): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-18 12:32:29.516: ERROR/AndroidRuntime(601): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-18 12:32:29.516: ERROR/AndroidRuntime(601): at dalvik.system.NativeStart.main(Native Method)
07-18 12:32:29.516: ERROR/AndroidRuntime(601): Caused by: java.lang.NullPointerException
07-18 12:32:29.516: ERROR/AndroidRuntime(601): at android.app.Activity.setContentView(Activity.java:1647)
07-18 12:32:29.516: ERROR/AndroidRuntime(601): at android.app.ListActivity.ensureList(ListActivity.java:314)
07-18 12:32:29.516: ERROR/AndroidRuntime(601): at android.app.ListActivity.getListView(ListActivity.java:299)
07-18 12:32:29.516: ERROR/AndroidRuntime(601): at com.firstapp.MyPackage.ActivityTwo.<init>(ActivityTwo.java:23)
07-18 12:32:29.516: ERROR/AndroidRuntime(601): at java.lang.Class.newInstanceImpl(Native Method)
07-18 12:32:29.516: ERROR/AndroidRuntime(601): at java.lang.Class.newInstance(Class.java:1429)
07-18 12:32:29.516: ERROR/AndroidRuntime(601): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-18 12:32:29.516: ERROR/AndroidRuntime(601): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
ActivityTwo的oncreate()方法如下:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ArrayList<PackageInfo> installedPkg = new ArrayList<PackageInfo>();
installedPkg = getIntent().getParcelableArrayListExtra ("userPackageList");
addItemsToList(installedPkg);
setContentView(R.layout.main);
}
答案 0 :(得分:1)
修改强>
根据您的说明,您的ActivityTwo
看起来像这样:
public class ActivityTwo extends ListActivity {
public ListView listView = getListView();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<PackageInfo> installedPkg = new ArrayList<PackageInfo>();
installedPkg = getIntent().getParcelableArrayListExtra ("userPackageList");
addItemsToList(installedPkg);
setContentView(R.layout.main);
}
//Other functions follow
}
我认为异常是由public ListView listView = getListView();
行引起的。也许您可以尝试将代码重写为以下内容:
public class ActivityTwo extends ListActivity {
public ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = getListView();
}
@Override
public void onResume() {
ArrayList<PackageInfo> installedPkg = new ArrayList<PackageInfo>();
installedPkg = getIntent().getParcelableArrayListExtra ("userPackageList");
addItemsToList(installedPkg);
}
//Other functions follow
}
<强> ORIGIN:强>
也许我是愚蠢的,但在您的代码示例中,您将编辑意图定义为:
Intent editIntent = new Intent(getApplicationContext(), AddToList.class);
但我在manifest.xml中没有看到任何AddToList
活动定义(您有ActivityOne
和ActivityTwo
)
还有一种情况需要注意getApplicationContext()
。 Using Application context everywhere?
答案 1 :(得分:0)
你好在你的清单文件中添加你的活动名称正确地标记了通信的名称。
这里假设您的活动名称是AddToList.java
活动android:name =“。AddToList” 机器人:标签= “@串/ APP_NAME”强>
但是在你的menfest ur活动名称你写的:ActivityTwo 所以在清单文件中尝试标记
答案 2 :(得分:0)
只需使用即可查看R.id值。您使用的所有值必须出现在setContentview
中设置的布局中