BadParcelableException:启动器快捷方式

时间:2014-08-20 11:24:56

标签: java android android-intent bundle parcel

我尝试将自定义Object传递到我在主屏幕上安装的快捷方式,但是我很快就收到了BadParcelableException快捷方式已创建。我尝试过调用Bundle.setClassLoader,但错误仍然存​​在。

支持主屏幕快捷方式的发射器是否有限制?

public class Foo implements Parcelable {

    public String title;

    private Foo(Parcel in) {
        title = in.readString();
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(title);
    }

    public static final Parcelable.Creator<Foo> CREATOR = new Parcelable.Creator<Foo>() {

        @Override
        public Foo createFromParcel(Parcel in) {
            return new Foo(in);
        }

        @Override
        public Foo[] newArray(int size) {
            return new Foo[size];
        }

    };

}

public static void installShortcut(Context context, Foo foo) {
    final Bundle args = new Bundle();
    args.setClassLoader(Foo.class.getClassLoader());
    args.putParcelable(EXTRA_FOO, foo);
    final Intent detail = new Intent(context, FooActivity.class);
    detail.putExtras(args);

    final Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, detail);
    ...
    context.sendBroadcast(shortcut);
}

0 个答案:

没有答案