1

BadParcelableException在尝试从Bundle.

有人知道为什么会这样吗?我认为它应该总是发生,或者永远不会发生,而不是随机发生。这是我的代码:

  1. 我要放入 Bundle 的 Parcelable 对象:

    public interface ButtonPressedCallback extends Parcelable {
        public void onButtonPressed ();
    }
    
  2. 这就是我把它放在Bundle中的方式

    bundle.putString(MyFragment.TITLE, title);
    bundle.putParcelable(MyFragment.CALLBACK , callback);
    
  3. 这就是我检索它的方式(在这里它崩溃了)

    public class MyFragment extends Fragment {    
        private String title;
        private ButtonPressedCallback callback;
    
        public static final String TITLE = "TITLE";
        public static final String CALLBACK = "CALLBACK";
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            Bundle b = getArguments();
    
            title = getArguments().getString(TITLE); // it crashes here
            callback = b.getParcelable(CALLBACK );
            ...
       }
    

它在检索普通字符串 TITLE 时神秘地在线崩溃(奇怪....),但我 100% 确定该行没问题,它实际上在下面崩溃 - 因为它是在我将接口添加到 Bundle 时开始的

这是堆栈跟踪:

Caused by: android.os.BadParcelableException: 
  at android.os.Parcel.readParcelableCreator (Parcel.java:2875)
  at android.os.Parcel.readParcelable (Parcel.java:2797)
  at android.os.Parcel.readValue (Parcel.java:2700)
  at android.os.Parcel.readArrayMapInternal (Parcel.java:3067)
  at android.os.BaseBundle.unparcel (BaseBundle.java:257)
  at android.os.BaseBundle.getString (BaseBundle.java:1086)
       at sk.myapp.intro.MyFragment.onCreate (MyFragment.java:42)
  at android.support.v4.app.Fragment.performCreate (Fragment.java:2414)
  at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManagerImpl.java:1418)
  at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState (FragmentManagerImpl.java:1784)
  at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManagerImpl.java:1852)
  at android.support.v4.app.FragmentManagerImpl.dispatchStateChange (FragmentManagerImpl.java:3269)
  at android.support.v4.app.FragmentManagerImpl.dispatchCreate (FragmentManagerImpl.java:3223)
  at android.support.v4.app.FragmentController.dispatchCreate (FragmentController.java:190)
  at android.support.v4.app.FragmentActivity.onCreate (FragmentActivity.java:369)
       at sk.myapp.intro.MainActivity.onCreate (MainActivity.java:46)
  at android.app.Activity.performCreate (Activity.java:7183)
  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1221)
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2910)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3032)
  at android.app.ActivityThread.-wrap11 (Unknown Source)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1696)
  at android.os.Handler.dispatchMessage (Handler.java:105)
  at android.os.Looper.loop (Looper.java:164)
  at android.app.ActivityThread.main (ActivityThread.java:6942)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:327)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1374)
4

2 回答 2

1

写这个是因为我无法在评论中提供示例,

@SuppressLint("ParcelCreator")
public class MainActivity extends AppCompatActivity implements CallbackInterface{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Bundle bundle = new Bundle();
        bundle.putParcelable("CALLBACK",this); // <---

    }

    @Override
    public void demoCallback() {

    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {

    }
}

由于我正在通过this,当我的 Activity 包含大量成员变量(> 1MB)时它可能会崩溃

于 2019-06-20T12:06:00.380 回答
0

Astha这是在回调中(基本上只是通过按钮在主屏幕上添加widgte):

AppWidgetManager appWidgetManager =
        context.getSystemService(AppWidgetManager.class);
ComponentName myProvider =
        new ComponentName(context, MyAppWidgetProvider.class);

if (appWidgetManager.isRequestPinAppWidgetSupported()) {
    // Create the PendingIntent object only if your app needs to be notified
    // that the user allowed the widget to be pinned. Note that, if the pinning
    // operation fails, your app isn't notified.
    Intent pinnedWidgetCallbackIntent = new Intent( ... );

    // Configure the intent so that your app's broadcast receiver gets
    // the callback successfully. This callback receives the ID of the
    // newly-pinned widget (EXTRA_APPWIDGET_ID).
    PendingIntent successCallback = PendingIntent.getBroadcast(context, 0,
            pinnedWidgetCallbackIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    appWidgetManager.requestPinAppWidget(myProvider, null, successCallback);
}
于 2019-06-19T06:31:40.157 回答