2

我收到此错误:

                FATAL EXCEPTION: main
        java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = game.Level)
        at android.os.Parcel.writeSerializable(Parcel.java)
        at android.os.Parcel.writeValue(Parcel.java)
        at android.os.Parcel.writeMapInternal(Parcel.java)
        at android.os.Bundle.writeToParcel(Bundle.java)
        at android.os.Parcel.writeBundle(Parcel.java)
        at android.content.Intent.writeToParcel(Intent.java)
        at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java)
        at android.app.Activity.startActivityForResult(Activity.java)
        at android.app.Activity.startActivityForResult(Activity.java)
        at android.app.Activity.startActivity(Activity.java)
        at android.app.Activity.startActivity(Activity.java)
        at menu.AreaAdapter$1.onClick(AreaAdapter.java:93)
        at android.view.View.performClick(View.java)
        at android.view.View$PerformClick.run(View.java)
        at android.os.Handler.handleCallback(Handler.java)
        at android.os.Handler.dispatchMessage(Handler.java)
        at android.os.Looper.loop(Looper.java)
        at android.app.ActivityThread.main(ActivityThread.java)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.io.NotSerializableException: menu.AreaActivity
        at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
        at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
        at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
        at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
        at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
        at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
        at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)

运行时:

holder.butArea.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent myIntent = new Intent(v.getContext(), game.LevelActivity.class);
                myIntent.putExtra("switchlevel", LevelSwitchCase.getLevel(currentLevel, (pos+1),mContext));
                myIntent.putExtra("level",currentLevel);
                myIntent.putExtra("area", (pos+1));

                v.getContext().startActivity(myIntent);

            }
        });

game.level 正在实现可序列化并在启动意图时收到错误 (v.getContext().startActivity(myIntent);)

level 和 area 只是整数,而 switchlevel 是我得到的一个级别(实现 Serializable)

4

3 回答 3

6

上下文不可序列化。为了解决这个错误,我声明了这样的上下文(变量不序列化):

  public transient Context mContext;

或者你可以实现 Parceable。

于 2013-05-07T17:14:10.053 回答
4

如果你的关卡类有非原始成员,它们也应该实现可序列化。

于 2013-05-06T17:40:22.090 回答
0

对于那些在接口内使用上下文或其他不可序列化对象的人。只需将其作为参数传递给方法,例如:

public interface OnActionInterface {
    void onPositiveButtonClicked(Activity activity);
    void onNegativeButtonClicked(Activity activity);
}

然后创建一个独立的类,如:

public class SomeClass implements OnActionInterface, Serializable {
    private String action;

    public SomeClass(String action) {
        this.action = action;
    }

    @Override
    public void onPositiveButtonClicked(Activity activity) {
        Intent intent = new Intent(action);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        activity.startActivity(intent);
    }

    @Override
    public void onNegativeButtonClicked(Activity activity) {
        activity.finish();
    }
}

之后,您可以在意图中使用此类的对象。

于 2016-05-06T11:40:28.800 回答