当我正确传递时,getExtras方法检索null对象

时间:2011-01-14 12:47:50

标签: android android-intent bundle

我在两个活动之间传递一些数据,但是我得到一个NullPointerException,我不应该...

我开始这样的活动

Intent i= new Intent(getApplicationContext(),
     NewActivity.class);

i.putExtra(SERVICE_ID, serviceId);
i(serviceActivityIntent);

我试图以这种方式获得另一方的serviceId

    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
      }

我想我已经把一切都做好了,我在这里找不到线索...... 这是我得到的例外:

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: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

显然,当第二个活动开始时,意图中的对象“extras”为null,但我一直想知道为什么......

顺便说一下,GroupServiceId实现了Serializable,这不是问题,或者我认为是这样......

由于

2 个答案:

答案 0 :(得分:3)

你的额外包不是null,它是GroupServiceId.readObject()崩溃了。你的序列化搞砸了。建议您发布GroupServiceId的代码。

答案 1 :(得分:0)

我认为您需要使用extras.putSerializable()方法添加对象,然后在获取它时可以将其强制转换。这是上一个问题link

的链接