为什么getArguments()。getParcelable(key)在本地测试设备和生产版本中的行为有所不同?

时间:2014-04-23 10:14:42

标签: java android android-fragments parcelable

在我的MainActivity中,我调用此代码更改为新片段,我将带有parcelable对象itemModel的包传递给片段:

    Bundle bundle = new Bundle();

    bundle.putParcelable(ItemModel.ParcelableKey, itemModel);
    fragment = new AppFragment();
    fragment.setArguments(bundle);

    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.frame_container, fragment).addToBackStack(null).commit();

在片段中,我像这样收到包裹:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)    {
    super.onCreate(savedInstanceState);

    mItemModel = getArguments().getParcelable(ItemModel.ParcelableKey);
    //....other code.....
}

当我在本地测试时,代码在我的测试设备上运行正常,mItemModel能够正确检索回来,但是,我已将崩溃报告系统集成到应用程序(Crashlytics)中,并且报告指出发行版上的这样的日志:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.temp.test/com.temp.test.MainActivity}: java.lang.NullPointerException
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
       at android.app.ActivityThread.access$700(ActivityThread.java:143)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:137)
       at android.app.ActivityThread.main(ActivityThread.java:4960)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:511)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
       at dalvik.system.NativeStart.main(NativeStart.java)
Caused by: java.lang.NullPointerException
       at android.os.Parcel.readStringArray(Parcel.java:931)
       at com.temp.test.data.ItemModel.<init>(ItemModel.java:478)
       at com.temp.test.data.ItemModel.<init>(ItemModel.java:23)
       at com.temp.test.data.ItemModel$1.createFromParcel(ItemModel.java:445)
       at com.temp.test.data.ItemModel$1.createFromParcel(ItemModel.java:443)
       at android.os.Parcel.readParcelable(Parcel.java:2103)
       at android.os.Parcel.readValue(Parcel.java:1965)
       at android.os.Parcel.readMapInternal(Parcel.java:2226)
       at android.os.Bundle.unparcel(Bundle.java:223)
       at android.os.Bundle.getParcelable(Bundle.java:1165)
       at com.temp.test.fragments.AppFragment.onCreateView(AppFragment.java:98)
       at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
       at android.app.FragmentManagerImpl.moveTo## Heading ##State(FragmentManager.java:1035)
       at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1017)
       at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1804)
       at android.app.Activity.performCreate(Activity.java:5206)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
       at android.app.ActivityThread.access$700(ActivityThread.java:143)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:137)
       at android.app.ActivityThread.main(ActivityThread.java:4960)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:511)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
       at dalvik.system.NativeStart.main(NativeStart.java)

at com.temp.test.fragments.AppFragment.onCreateView(AppFragment.java:98)&lt; - 此错误行导致源代码中的这一行:

mItemModel = getArguments().getParcelable(ItemModel.ParcelableKey);

这很奇怪,因为它在我的本地设备中运行良好。所以我更深入地研究了Bundle的源代码,我注意到在unparcel()中,它在开头就有这样的检查:

synchronized void unparcel() {

    if (mParcelledData == null) {
        if (DEBUG) Log.d(TAG, "unparcel " + Integer.toHexString(System.identityHashCode(this)) + ": no parcelled data");
        return;
    }
    //....other code.....
}

奇怪的是,在我的情况下,mParcelledData应该是null,因为我创建了Bundle这样Bundle bundle = new Bundle();,如上面的代码片段所示,所以在我的如果它应该使用mMap而不是mParcelledData

问题:getArguments()fragment's onCreateView()将返回Bundle非空mParcelledData的可能情况是什么?

OR

mItemModel = getArguments().getParcelable(ItemModel.ParcelableKey);在我的本地测试设备与最终用户的生产版本中的执行方式有何不同?

0 个答案:

没有答案