getExtras method retrieves a null object when I passed it correctly
I'm passing some data between two activities but I'm getting a NullPointerException where I shouldn't...
I start the activity like this
Intent i= new Intent(getApplicationContext(),
NewActivity.class);
i.putExtra(SERVICE_ID, serviceId);
i(serviceActivityIntent);
And I try to get the serviceId on the other side in this fashion
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* I also tried this just for in case my object was in the savedInstanceState */
// mServiceId = (savedInstanceState == null) ? null
// : (GroupServiceId) savedInstanceState
// .getSerializable(ServicesActivity.SERVICE_ID);
if (mServiceId == null) {
Bundle extras = getIntent().getExtras();
mServiceId = extras != null ? (GroupServiceId) extras
.getSerializable(ServicesActivity.SERVICE_ID) : null; // <-- this is line 86
}
I think I've done everything okay and I'm running out of clues here... Here's the exception I get:
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): FATAL EXCEPTION: main
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.pfc/org.pfc.NewActivity}: java.lang.NullPointerException
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): at android.app.ActivityThread.access$2300(ActivityThread.java:135)
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): at android.os.Handler.dispatchMessage(Handler.java:99)
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): at android.os.Looper.loop(Looper.java:144)
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): at android.app.ActivityThread.main(ActivityThread.java:4937)
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): at java.lang.reflect.Method.invokeNative(Native Method)
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): at java.lang.reflect.Method.invoke(Method.java:521)
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): at dalvik.system.NativeStart.main(Native Method)
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): Caused by: java.lang.NullPointerException
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): at org.smepp.datatypes.smm.GroupServiceId.readObject(GroupServiceId.java:105)
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): at java.lang.reflect.Method.invokeNative(Native Method)
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): at java.lang.reflect.Method.invoke(Method.java:521)
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1537)
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1460)
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:2139)
01-14 13开发者_JS百科:11:57.735: ERROR/AndroidRuntime(1019): at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:929)
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2285)
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2240)
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): at android.os.Parcel.readSerializable(Parcel.java:1945)
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): at android.os.Parcel.readValue(Parcel.java:1822)
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): at android.os.Parcel.readMapInternal(Parcel.java:2008)
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): at android.os.Bundle.unparcel(Bundle.java:208)
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): at android.os.Bundle.getSerializable(Bundle.java:1189)
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): at org.pfc.NewActivity.onCreate(NewActivity.java:86)
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
01-14 13:11:57.735: ERROR/AndroidRuntime(1019): ... 11 more
Obviously, the object "extras" from the intent is null when the second activity starts, but I keep wondering why...
By the way, GroupServiceId implements Serializable so that's not the problem, or I think so...
Thanks
Your extras bundle isn't null
, it's GroupServiceId.readObject()
that's crashing. Your serialization is screwed. Suggest you post code for GroupServiceId
.
I Think you need to add your object using the extras.putSerializable() method and then you can cast it when you get it back. Here's a link to a previous question link
精彩评论