活动启动时的ClassNotFoundException的序列化类 [英] ClassNotFoundException on serializable class during activity start

查看:208
本文介绍了活动启动时的ClassNotFoundException的序列化类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的设置

我)的活动,只有覆盖的onCreate(),onResume(和的onSaveInstanceState()。的onSaveInstanceState中的(),我把一个序列化对象:

I have an activity that only overrides onCreate(), onResume(), and onSaveInstanceState(). In onSaveInstanceState(), I put in a serializable object:

    @Override 
    public void onSaveInstanceState(Bundle state) {
        super.onSaveInstanceState(state);
        state.putSerializable("obj", myObj); // myObj is of class MyClass               state.putLong("long", longVar);
    }

MyClass的原来是活动类中的内部类,但对于调试,我甚至把它移到一个单独的文件:

MyClass was originally an inner class inside the activity class, but for debugging, I even moved it to a separate file:

public class MyClass implements Serializable {
    private static final long serialVersionUID = 0x98ED2F00;
     ....
}

的步骤:


  1. 开始用通常的方法程序,它运行良好。

  2. 单击Home键回到启动器。

  3. 从Eclipse中,在设备面板停止进程

  4. 再从启动启动程序,这一次我得到如下:

  1. Start the program in the usual way, it runs fine.
  2. Click Home button to go back to the Launcher.
  3. From Eclipse, stop the process from the Device panel
  4. Start the program again from Launcher, this time I get the following:

了java.lang.RuntimeException:无法启动活动ComponentInfo {} com.example.Reports:了java.lang.RuntimeException Parcelable遇到ClassNotFoundException的读取序列化对象{名= com.example.MyClass}

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.Reports}: java.lang.RuntimeException Parcelable encountered ClassNotFoundException reading a serializable object {name = com.example.MyClass}

致:抛出java.lang.ClassNotFoundException:[] com.examp.MyClass装载机dalvik.System.PathClassLoader

Caused by: java.lang.ClassNotFoundException: com.examp.MyClass in loader dalvik.System.PathClassLoader[.]

线索和意见:


  1. 最重要的线索是:如果我不救MyObj中的onSaveInstanceState中的(),那么一切都很好。而且,把任何原始类型到捆绑是好的,但不是我的对象。

  1. The most important clue is: if I don't save myObj in onSaveInstanceState(), then everything is fine. And, putting any primitive types into the Bundle is fine, but not my objects.

的异常被抛出我的code以外,它发生的onCreate()和onResume()之间。这里面发生的Andr​​oid。

The exception is thrown outside of my code, it happened between onCreate() and onResume(). It happened inside Android.

在错误消息中的类名是正确的:com.example.MyClass。最初它是活动中的内部类,而是要找出问题,我把它移到一个单独的文件,但没有成功。

The class name in the error message is correct: com.example.MyClass. Originally it was an inner class inside the activity, but to isolate the problem, I moved it to a separate file, to no avail.

我相信序列化的实施是正确的,因为它可以从程序的其他部分文件被序列化到/。

I believe the implementation of Serializable is correct, because it can be serialized to/from a file in other parts of the program.

有谁有这样的可能的原因的想法?大部分AP preciated! (对不起,我不能把详细的code在这里,因为一切都在这里交织和那里。我只能孤立尽可能。)

Does anybody have an idea of any possible cause of this? Much appreciated! (Sorry I can't put detailed code here, because everything is intertwined here and there. I can only isolate as much as possible.)

推荐答案

OK,我想我找到了解决这个问题。

OK, I think I found a solution to this problem.

原来,如果传入的onCreate()的savedInstanceState参数不为空,那么就必须反序列化包含在捆绑任何对象。我不知道为什么,但这是我如何摆脱这个问题。原来在我的onCreate code()有一些情况下,我不顾savedInstanceState的逻辑。现在我要做的就是,只要它不为空,我就反序列化所有的东西,然后根据其他条件,我决定如果我使用它们。

It turned out that if the savedInstanceState parameter passed into onCreate() is not null, then one MUST deserialize any objects contained in that Bundle. I don't know why, but this is how I got rid of the problem. Originally my code in onCreate() has the logic that under some conditions, I disregard the savedInstanceState. Now what I do is as long as it's not null, I'll just deserialize everything out, then, based on other conditions, I decide if I use them or not.

到目前为止运行良好。但我不知道的想法:

So far it runs fine. But I have no idea about:


  1. 为什么要反序列化出来,如果​​我不这样做,发生异常?

  1. Why do I have to deserialize them out, and if I don't, exception happens?

什么的Andr​​oid做反序列化我的东西,我把savedInstanceState?我的意思是,据我所知,Android的可能已经把一些信息供自己使用,但为什么它来试试反序列化我的东西?如果它知道它不知道如何使用它们?

What was Android doing to deserialize my things I put in savedInstanceState? I mean, I understand that Android may have put some information for its own use, but why does it come and try to deserialize my things? If it knows it doesn't know how to use them?

这篇关于活动启动时的ClassNotFoundException的序列化类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆