读取包时ClassNotFoundException

时间:2012-03-13 20:03:08

标签: android exception

我正在尝试从包裹中恢复我的应用程序(保存在捆绑包中)。

我的活动使用OpenGL,因此它会创建此表面视图,并在保存或恢复应用程序时调用这些函数。

class MySurfaceView extends GLSurfaceView {
    /* Lots of other stuff */
    public void onRestoreInstanceState(Bundle inState) {
        Log.d("Wormhole", "Restoring instance state");
        mRenderer.onRestoreInstanceState(inState);
    }

    public void onSaveInstanceState(Bundle outState) {
        Log.d("Wormhole", "Saving instance state");
        mRenderer.onSaveInstanceState(outState);
    }
}
mRenderer中的

public void onRestoreInstanceState(Bundle inState){
    mFlowManager = inState.getParcelable("flowmanager");
}

public void onSaveInstanceState (Bundle outState){
    outState.putParcelable("flowmanager", mFlowManager);
}

在mFlowManager中

public class FlowManager implements Touchable, Parcelable {
private enum State {
    SPLASH, MENU, GAME_SINGLE, GAME_MULTI
};

private Connection mConnection;
private ScoreDataSource mScoreDataSource;
private GameEngine mGameEngine;
private SplashScreen mSplash;
private MainMenu mMenu;
private State mState = State.SPLASH;
private long mTime;
private int mVersionID;

/* Other stuff */

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

@Override
public void writeToParcel(Parcel out, int flags) {
    out.writeString(mState.name());
    out.writeParcelable(mSplash, 0);
    out.writeParcelable(mMenu, 0);
}

public static final Parcelable.Creator<FlowManager> CREATOR = new Parcelable.Creator<FlowManager>() {
    public FlowManager createFromParcel(Parcel in) {
        return new FlowManager(in);
    }

    public FlowManager[] newArray(int size) {
        return new FlowManager[size];
    }
};

private FlowManager(Parcel in) {
    mConnection = new Connection();

    mState = State.valueOf(in.readString());
    mSplash = in.readParcelable(null); // Exception occurs here
    mMenu = in.readParcelable(null);
}

}

FlowManager类包含需要保存的其他类的实例。那些我制作Parselable的类,恢复它们时我得到了错误。

我看过有关此错误的帖子,但它们都是用于在应用之间发送数据并且必须使用不同的ClassLoader。这是所有相同的应用程序。我是否需要设置我的ClassLoader,因为它在GLSurfaceView中?我如何找到我需要的ClassLoader?

1 个答案:

答案 0 :(得分:5)

更新您的FlowManager(Parcel in),如下所示:

private FlowManager(Parcel in) {
    mConnection = new Connection();

    mState = State.valueOf(in.readString());
    mSplash = in.readParcelable(SplashScreen.class.getClassLoader());
    mMenu = in.readParcelable(MainMenu.class.getClassLoader());
}