Parcelable Object return BadParcelableException: ClassNotFoundException when obj = getIntent().getParcelableExtra(“name”)

  • Replies:1
LadyChocobo
  • Forum posts: 1

Nov 18, 2015, 4:51:27 AM via Website

I have a problem when I write pracelable object and receive from another application that I wrote. Everytime when I tried to get the object it always turned out to be BadParcelableException: ClassNotFoundException when unmarshalling.

I've been looking around and wonder if it turned out bad when I send to another application. Or it's simply I had error in my codes.

Please tell me where did I go wrong.

Note: please understand that I have been looking for answer for this for a while and I still can't find where is my problem is. I looked up many people's code and tested out many times but still can't get my app right. So that is why I am here to ask help. If some people still think I am a lazy noob, I guess I am too lazy for some people to draw that conclusion. I should reconsider of my action again.

My situation is:
I wrote 2 apps and one is sending a parcelable object to another application. First app it's working correctly to send the object to another. However, as the second app receive it, it goes wrong and gives out the BadParcelableException as I run the process. I am not using bundle because I saw many examples that it is not necessary to do so. If I have been wrong please correct me. Both app contains the ParcelableObject class.

Send object lines:

Intent intent = new Intent();
ParcelableObjectmyObject = newParcelableObject();
myObject = newParcelableObject(result_name, resut_lv, result_race,
result_job, result_nation, result_guild, result_mission);
intent.putExtra("ACTION_DATA_TRANSFER",myObject);
intent.setClassName(target_app, target_app+".ReceiveMain");
startActivity(intent);

First app Manifest:

<manifest xmlns:android="schemas.android.com/apk/res/android"
package="abc.com.android.send_app"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".SendMain"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Receive code line:

ParcelableObjectmyObject = newParcelableObject();
myObject = getIntent().getParcelableExtra("ACTION_DATA_TRANSFER");//always crashed at this line

if(getIntent().hasExtra("ACTION_DATA_TRANSFER")){
    Log.w("****CHECKING****",myObject.getName());
    item1.setText("The Character's name is: "+myObject.getName());
}else{
    Toast.makeText(getApplicationContext(),"There is not data passed yet",
    Toast.LENGTH_LONG).show();
}

Second app Manifest:

<manifest xmlns:android="schemas.android.com/apk/res/android"
    package="def.com.android.receive_app"
    android:versionCode="1"
    android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />
 <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".ReceiveMain"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

This is my ParcelableObject class that implements Parcelable:

private String name;
private int lv;
private String race;
private String job;
private String nation;
private String guild;
public String mission;
public void setName(String name) {
    this.name = name;
}
public void setLv(int lv) {
    this.lv = lv;
}
public void setRace(String race) {
    this.race = race;
}
public void setJob(String job) {
    this.job = job;
}
public void setNation(String nation) {
    this.nation = nation;
}
public void setGuild(String guild) {
    this.guild = guild;
}
public void setMission(String mission) {
    this.mission = mission;
}
public ParcelableObject(String name, int lv, String race, String job, String
 nation, String guild, String mission) {
// TODO Auto-generated constructor stub
    this.name = name;
    this.lv = lv;
    this.race = race;
    this.job = job;
    this.nation = nation;
    this.guild = guild;
    this.mission = mission;
}
public String getName() {
    return name;
}
public int getLv() {
    return lv;
}
public String getRace() {
    return race;
}
public String getJob() {
    return job;
}
public String getNation() {
    return nation;
}
public String getGuild() {
    return guild;
}
public String getMission() {
    return mission;
}
@Override
public int describeContents() {
    // TODO Auto-generated method stub
    Log.w("****PO CHECKING****", "describeContents called");
    return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
    // TODO Auto-generated method stub
    Log.w("****PO CHECKING****", "writeToParcel called");
    dest.writeString(name);
    dest.writeString(job);
    dest.writeInt(lv);
    dest.writeString(race);
    dest.writeString(guild);
    dest.writeString(nation);      
    dest.writeString(mission);
}
public ParcelableObject() {
    // TODO Auto-generated constructor stub
    super();
    Log.i("**check if exist**", "start parcel");
}
public static final Parcelable.Creator<ParcelableObject> CREATOR =
    new Parcelable.Creator<ParcelableObject>() {
    @Override
    public ParcelableObject createFromParcel(Parcel in) {
        // TODO Auto-generated method stub
        Log.w("checking out", "createFromParcel");
        pObject.name = in.readString();
        pObject.job = in.readString();
        pObject.lv = in.readInt();
        pObject.race = in.readString();
        pObject.guild = in.readString();
        pObject.nation = in.readString();
        pObject.mission = in.readString();
        return pObject;
    }
    @Override
    public ParcelableObject[] newArray(int size) {
        // TODO Auto-generated method stub
        return new ParcelableObject[size];
    }
};

Error Log:

11-18 09:52:39.444: E/Parcel(29463): Class not found when unmarshalling: abc.com.android.send_app.ParcelableObject
11-18 09:52:39.444: E/Parcel(29463): java.lang.ClassNotFoundException: abc.com.android.send_app.ParcelableObject
11-18 09:52:39.444: E/Parcel(29463):     at java.lang.Class.classForName(Native Method)
11-18 09:52:39.444: E/Parcel(29463):     at java.lang.Class.forName(Class.java:251)
11-18 09:52:39.444: E/Parcel(29463):     at android.os.Parcel.readParcelableCreator(Parcel.java:2133)
11-18 09:52:39.444: E/Parcel(29463):     at android.os.Parcel.readParcelable(Parcel.java:2097)
11-18 09:52:39.444: E/Parcel(29463):     at android.os.Parcel.readValue(Parcel.java:2013)
11-18 09:52:39.444: E/Parcel(29463):     at android.os.Parcel.readArrayMapInternal(Parcel.java:2314)
11-18 09:52:39.444: E/Parcel(29463):     at android.os.Bundle.unparcel(Bundle.java:249)
11-18 09:52:39.444: E/Parcel(29463):     at android.os.Bundle.getParcelable(Bundle.java:1206)
11-18 09:52:39.444: E/Parcel(29463):     at android.content.Intent.getParcelableExtra(Intent.java:4652)
11-18 09:52:39.444: E/Parcel(29463):     at def.com.android.receive_app.ReceiveMain.onCreate(ReceiveMain.java:23)
11-18 09:52:39.444: E/Parcel(29463):     at android.app.Activity.performCreate(Activity.java:5231)
11-18 09:52:39.444: E/Parcel(29463):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-18 09:52:39.444: E/Parcel(29463):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
11-18 09:52:39.444: E/Parcel(29463):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
11-18 09:52:39.444: E/Parcel(29463):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-18 09:52:39.444: E/Parcel(29463):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-18 09:52:39.444: E/Parcel(29463):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-18 09:52:39.444: E/Parcel(29463):     at android.os.Looper.loop(Looper.java:136)
11-18 09:52:39.444: E/Parcel(29463):     at android.app.ActivityThread.main(ActivityThread.java:5001)
11-18 09:52:39.444: E/Parcel(29463):     at java.lang.reflect.Method.invokeNative(Native Method)
11-18 09:52:39.444: E/Parcel(29463):     at java.lang.reflect.Method.invoke(Method.java:515)
11-18 09:52:39.444: E/Parcel(29463):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-18 09:52:39.444: E/Parcel(29463):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-18 09:52:39.444: E/Parcel(29463):     at dalvik.system.NativeStart.main(Native Method)
11-18 09:52:39.444: E/Parcel(29463): Caused by: java.lang.NoClassDefFoundError: abc/com/android/send_app/ParcelableObject
11-18 09:52:39.444: E/Parcel(29463):     ... 24 more
11-18 09:52:39.444: E/Parcel(29463): Caused by: java.lang.ClassNotFoundException: Didn't find class "abc.com.android.send_app.ParcelableObject" on path: DexPathList[[zip file "/data/app/def.com.android.receive_app-2.apk"],nativeLibraryDirectories=[/data/app-lib/def.com.android.receive_app-2, /vendor/lib, /system/lib]]
11-18 09:52:39.444: E/Parcel(29463):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
11-18 09:52:39.444: E/Parcel(29463):     at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
11-18 09:52:39.444: E/Parcel(29463):     at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
11-18 09:52:39.444: E/Parcel(29463):     ... 24 more

Reply
dev ncow
  • Forum posts: 17

Dec 1, 2015, 10:59:58 AM via Website

Hello,

Parcable objects are not very convenient. I have used these kind of objects in previous apps and it was a pain ...
I move to JSON objects to communicate between Activities and (in your case) between apps. No need to share same object between apps because you manage only JSON / String.

I guess you would have a lot of refactoring to do if you move to JSON but I think it is a good way overall if you are stucked for a while.

What do you think about it ?

Regards,

Reply