Android java.lang.RuntimeException:Parcelable在读取Serializable对象时遇到ClassNotFoundException

时间:2018-05-16 03:57:27

标签: java android

我正在尝试将Serializable对象从一个应用程序发送到另一个应用程序。

我的Android应用程序有一个可序列化的类

public class MyClass implements Serializable

然后我把它的一个实例放到了意图

MyClass myObject = new MyClass();
intent.putExtra("payItem", myObject);
intent.setAction("com.mypackage.some.action");

当我致电startActiviy

startActivity(intent);

我的应用程序崩溃了

5-16 11:09:24.738 30673-30673/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: system:ui, PID: 30673
java.lang.RuntimeException: Unable to start activity ComponentInfo{android/com.android.internal.app.ResolverActivity}: java.lang.RuntimeException: Parcelable encountered ClassNotFoundException reading a Serializable object (name = com.mypackage.MyClass)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2955)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    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:6938)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
 Caused by: java.lang.RuntimeException: Parcelable encountered ClassNotFoundException reading a Serializable object (name = com.mypackage.MyClass)
    at android.os.Parcel.readSerializable(Parcel.java:2956)
    at android.os.Parcel.readValue(Parcel.java:2742)
    at android.os.Parcel.readArrayMapInternal(Parcel.java:3058)
    at android.os.BaseBundle.unparcel(BaseBundle.java:257)
    at android.os.Bundle.getParcelable(Bundle.java:888)
    at android.content.Intent.getParcelableExtra(Intent.java:7734)
    at android.app.Activity.getReferrer(Activity.java:5398)
    at com.android.internal.app.ResolverActivity.getReferrerPackageName(ResolverActivity.java:554)
    at com.android.internal.app.ResolverActivity.onCreate(ResolverActivity.java:435)
    at com.android.internal.app.ResolverActivity.onCreate(ResolverActivity.java:331)
    at android.app.Activity.performCreate(Activity.java:7174)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    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:6938) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 
 Caused by: java.lang.ClassNotFoundException: com.mypackage.MyClass
    at java.lang.Class.classForName(Native Method)
    at java.lang.Class.forName(Class.java:453)
    at android.os.Parcel$2.resolveClass(Parcel.java:2942)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1616)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1521)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1777)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1354)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:374)
    at android.os.Parcel.readSerializable(Parcel.java:2950)
    at android.os.Parcel.readValue(Parcel.java:2742) 
    at android.os.Parcel.readArrayMapInternal(Parcel.java:3058) 
    at android.os.BaseBundle.unparcel(BaseBundle.java:257) 
    at android.os.Bundle.getParcelable(Bundle.java:888) 
    at android.content.Intent.getParcelableExtra(Intent.java:7734) 
    at android.app.Activity.getReferrer(Activity.java:5398) 
    at com.android.internal.app.ResolverActivity.getReferrerPackageName(ResolverActivity.java:554) 
    at com.android.internal.app.ResolverActivity.onCreate(ResolverActivity.java:435) 
    at com.android.internal.app.ResolverActivity.onCreate(ResolverActivity.java:331) 
    at android.app.Activity.performCreate(Activity.java:7174) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    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:6938) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 
 Caused by: java.lang.ClassNotFoundException: Didn't find class "com.mypackage.MyClass" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/system/lib64, /system/vendor/lib64, /system/lib64, /system/vendor/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at java.lang.Class.classForName(Native Method) 
    at java.lang.Class.forName(Class.java:453) 
    at android.os.Parcel$2.resolveClass(Parcel.java:2942) 
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1616) 
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1521) 
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1777) 
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1354) 
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:374) 
    at android.os.Parcel.readSerializable(Parcel.java:2950) 
    at android.os.Parcel.readValue(Parcel.java:2742) 
    at android.os.Parcel.readArrayMapInternal(Parcel.java:3058) 
    at android.os.BaseBundle.unparcel(BaseBundle.java:257) 
    at android.os.Bundle.getParcelable(Bundle.java:888) 
    at android.content.Intent.getParcelableExtra(Intent.java:7734) 
    at android.app.Activity.getReferrer(Activity.java:5398) 
    at com.android.internal.app.ResolverActivity.getReferrerPackageName(ResolverActivity.java:554) 
    at com.android.internal.app.ResolverActivity.onCreate(ResolverActivity.java:435) 
    at com.android.internal.app.ResolverActivity.onCreate(ResolverActivity.java:331) 
    at android.app.Activity.performCreate(Activity.java:7174) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    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:6938) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 

实际上我的手机已经安装了两个应用程序并注册了一个活动 使用相同的操作名称

<activity
        android:name="com.mypackage.MyActivity"
        android:exported="true"
        >
        <intent-filter>
            <action android:name="com.mypackage.some.action"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>

我希望显示一个对话框让我选择应该启动哪个活动,但我刚收到上面的错误。如果我卸载其中一个应用程序,则不会发生错误,并且它会按预期运行。

我的问题是

  1. 为什么没有显示对话框
  2. 是崩溃的原因
  3. 如果安装了两个应用程序,那么避免该错误的解决方案是什么

0 个答案:

没有答案