Null pointer exception while starting one activity from another
I am trying to start one activity from another on user selection from a context menu. The code for code context menu case is:
case R.id.organize:
开发者_StackOverflow社区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;
And my AndroidManifest.xml also has entries for both the activities:
<?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>
The output prints both the log statements in the switch case and then crashes with the following exception:
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)
The oncreate() method for the ActivityTwo is below:
@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);
}
EDIT:
According to your notes your ActivityTwo
looks something like this:
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
}
I think the exception is caused by the public ListView listView = getListView();
line. Maybe you could try to rewrite your code to something like this:
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:
Maybe I'm stupid or something but in your code example you're defining your edit intent as:
Intent editIntent = new Intent(getApplicationContext(), AddToList.class);
But I don't see any AddToList
activity-definition in your manifest.xml (you have ActivityOne
and ActivityTwo
)
Also there are situations where one should be careful with getApplicationContext()
. Using Application context everywhere?
hello in your manifest file add your Activity name Correctly thid tag name for communication.
Here Suppose your Activity name is AddToList.java
activity android:name=".AddToList" android:label="@string/app_name"
but in your menfest ur activity name you written : ActivityTwo so try tag in manifest file
Just check the R.id values u r using. All the values you use must be present in the layout that you set in the setContentview
精彩评论