Android E / Parcel:解组时未find类(仅适用于Samsung Tab3)

我一直无法解决为什么会出现这个错误,只有在运行4.4.2的三星Tab3设备? 这发生在我的MainActivity启动另一个Activity,并传递一个Parcelable类的意图,如下所示:

private void debugTest(TestParcel cfgOptions){ TestParcel cfgOptions = new TestParcel(); cfgOptions.setValue(15); //just to verify Intent intent = new Intent(MainActivity.this, TestActivity.class); intent.putExtra("cfgOptions", cfgOptions); startActivityForResult(intent, DBG_TEST); } 

TestActivity获取可见的数据,如下所示:

 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_activity); TestParcel cfgOptions = getIntent().getExtras().getParcelable("cfgOptions"); } 

TestParcel类:

  import android.os.Parcel; import android.os.Parcelable; public class TestParcel implements Parcelable { private long l_ucs_value = 0; private String s_rx_number = ""; //constructor public TestParcel() { l_ucs_value = 0; s_rx_number = ""; } public void RxNumber(String s) { s_rx_number = s; } public String RxNumber() { return s_rx_number; } //----------------------------------------------------------------------- public void setValue(long v){ l_ucs_value = v; } public long getValue(){ return l_ucs_value; } protected TestParcel(Parcel in) { l_ucs_value = in.readLong(); s_rx_number = in.readString(); } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeLong(l_ucs_value); dest.writeString(s_rx_number); } @SuppressWarnings("unused") public static final Parcelable.Creator<TestParcel> CREATOR = new Parcelable.Creator<TestParcel>() { @Override public TestParcel createFromParcel(Parcel in) { return new TestParcel(in); } @Override public TestParcel[] newArray(int size) { return new TestParcel[size]; } }; } 

再次,我只在三星的Tab3设备上看到这一点 – 但这是我们需要它的工作设备。 这里是三星logcat:

 02-18 08:05:55.393 2235-2571/? E/Parcel? Class not found when unmarshalling: com.vms.android.VersatileDEX.TestParcel java.lang.ClassNotFoundException: com.vms.android.VersatileDEX.TestParcel at java.lang.Class.classForName(Native Method) at java.lang.Class.forName(Class.java:251) at java.lang.Class.forName(Class.java:216) at android.os.Parcel.readParcelableCreator(Parcel.java:2133) at android.os.Parcel.readParcelable(Parcel.java:2097) at android.os.Parcel.readValue(Parcel.java:2013) at android.os.Parcel.readArrayMapInternal(Parcel.java:2314) at android.os.Bundle.unparcel(Bundle.java:249) at android.os.Bundle.getString(Bundle.java:1118) at android.content.Intent.getStringExtra(Intent.java:5148) at com.android.server.am.ActivityStackSupervisor.startActivityLocked(ActivityStackSupervisor.java:1467) at com.android.server.am.ActivityStackSupervisor.startActivityMayWait(ActivityStackSupervisor.java:1063) at com.android.server.am.ActivityManagerService.startActivityAsUser(ActivityManagerService.java:4134) at com.android.server.am.ActivityManagerService.startActivity(ActivityManagerService.java:4032) at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:159) at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2712) at android.os.Binder.execTransact(Binder.java:404) at dalvik.system.NativeStart.run(Native Method) Caused by: java.lang.NoClassDefFoundError: com/vms/android/VersatileDEX/TestParcel at java.lang.Class.classForName(Native Method) at java.lang.Class.forName(Class.java:251) at java.lang.Class.forName(Class.java:216) at android.os.Parcel.readParcelableCreator(Parcel.java:2133) at android.os.Parcel.readParcelable(Parcel.java:2097) at android.os.Parcel.readValue(Parcel.java:2013) at android.os.Parcel.readArrayMapInternal(Parcel.java:2314) at android.os.Bundle.unparcel(Bundle.java:249) at android.os.Bundle.getString(Bundle.java:1118) at android.content.Intent.getStringExtra(Intent.java:5148) at com.android.server.am.ActivityStackSupervisor.startActivityLocked(ActivityStackSupervisor.java:1467) at com.android.server.am.ActivityStackSupervisor.startActivityMayWait(ActivityStackSupervisor.java:1063) at com.android.server.am.ActivityManagerService.startActivityAsUser(ActivityManagerService.java:4134) at com.android.server.am.ActivityManagerService.startActivity(ActivityManagerService.java:4032) at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:159) at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2712) at android.os.Binder.execTransact(Binder.java:404) at dalvik.system.NativeStart.run(Native Method) Caused by: java.lang.ClassNotFoundException: Didn't find class "com.vms.android.VersatileDEX.TestParcel" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/vendor/lib, /system/lib]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:67) at java.lang.ClassLoader.loadClass(ClassLoader.java:497) at java.lang.ClassLoader.loadClass(ClassLoader.java:457) at java.lang.Class.classForName(Native Method) at java.lang.Class.forName(Class.java:251) at java.lang.Class.forName(Class.java:216) at android.os.Parcel.readParcelableCreator(Parcel.java:2133) at android.os.Parcel.readParcelable(Parcel.java:2097) at android.os.Parcel.readValue(Parcel.java:2013) at android.os.Parcel.readArrayMapInternal(Parcel.java:2314) at android.os.Bundle.unparcel(Bundle.java:249) at android.os.Bundle.getString(Bundle.java:1118) at android.content.Intent.getStringExtra(Intent.java:5148) at com.android.server.am.ActivityStackSupervisor.startActivityLocked(ActivityStackSupervisor.java:1467) at com.android.server.am.ActivityStackSupervisor.startActivityMayWait(ActivityStackSupervisor.java:1063) at com.android.server.am.ActivityManagerService.startActivityAsUser(ActivityManagerService.java:4134) at com.android.server.am.ActivityManagerService.startActivity(ActivityManagerService.java:4032) at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:159) at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2712) at android.os.Binder.execTransact(Binder.java:404) at dalvik.system.NativeStart.run(Native Method) 

Solutions Collecting From Web of "Android E / Parcel:解组时未find类(仅适用于Samsung Tab3)"

由于一些奇怪的原因,看起来类加载器没有正确设置。

TestActivity.onCreate()尝试以下方法之一:


 TestParcel cfgOptions = getIntent().getParcelableExtra("cfgOptions"); 

 Intent intent = getIntent(); intent.setExtrasClassLoader(TestParcel.class.getClassLoader()); TestParcel cfgOptions = intent.getParcelableExtra("cfgOptions"); 

 Bundle extras = getIntent().getExtras(); extras.setClassLoader(TestParcel.class.getClassLoader()); TestParcel cfgOptions = extras.getParcelable("cfgOptions"); 

或者,将parcelable包装成一个包:

 Bundle b = new Bundle(); b.putParcelable("options", cfgOptions); Intent intent = new Intent(MDex.this, TestActivity.class); intent.putExtra("bundle", b); 

要得到:

 Bundle b = getIntent().getBundleExtra("bundle"); TestParcel cfgOptions = b.getParcelable("options"); 

在我的情况下,新的Intent()。putExtra(key,parcelable)在内部自动创build新的捆绑给我unmarshall错误我不知道为什么,所以我不得不创build新的捆绑自己,添加parcelable,捆绑的意图。 这样做解决了这个问题。

只是试图在这里更简洁明了的答案 –

 //sending a parcelable class to another activity ----------------- MyParcelableOptionsClass mpoc = new MyParcelableOptionsClass(); Bundle b = new Bundle(); b.putParcelable("options", mpoc ); Intent intent = new Intent(MyActivity.this, OtherActivity.class); intent.putExtra("bundle", b); startActivityForResult(intent, 1); //getting the parcelable class from OtherActivity------------------ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.other_activity); Bundle b = getIntent().getBundleExtra("bundle"); MyParcelableOptionsClass mpoc = b.getParcelable("options"); } //returning the parcelable class back from OtherActivity ----------- Bundle b = new Bundle(); b.putParcelable("options", mpoc); Intent intent = new Intent(); intent.putExtra("bundle", b); setResult(0, intent); //and getting the parcelable class back in MyActivity -------------- onActivityResult(int requestCode, int resultCode, Intent data){ if(null != data){ Bundle b = data.getBundleExtra("bundle"); MyParcelableOptionsClass mpoc = b.getParcelable("options"); } }