I have two Android Application, where send object one application to another application.
when I share intent.putExtra("data", new CustomObject())
and also define in another application for received data intent.getSerializableExtra("data")
Getting error, when send data using custom object
Custom Class
Public class PatientObj implements Serializable {
public String getUserId() {
return UserId;
}
public void setUserId(String userId) {
UserId = userId;
}
public String getPatientId() {
return PatientId;
}
public void setPatientId(String patientId) {
PatientId = patientId;
}
public String getPatientName() {
return PatientName;
}
Getting Error
java.lang.RuntimeException: Parcelable encountered ClassNotFoundException reading a Serializable object (name = com.global.data.PatientObj)
at android.os.Parcel.readSerializable(Parcel.java:2952)
at android.os.Parcel.readValue(Parcel.java:2738)
at android.os.Parcel.readArrayMapInternal(Parcel.java:3054)
at android.os.BaseBundle.unparcel(BaseBundle.java:257)
at android.os.BaseBundle.getSerializable(BaseBundle.java:1162)
at android.os.Bundle.getSerializable(Bundle.java:982)
at android.content.Intent.getSerializableExtra(Intent.java:7261)
at com.app.android.ss_adv_type2.tevatronModule.connector.ProcessActivity.onCreate(ProcessActivity.java:40)
at android.app.Activity.performCreate(Activity.java:7383)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3256)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3411)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7529)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
Caused by: java.lang.ClassNotFoundException: com.global.data.PatientObj
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:453)
at android.os.Parcel$2.resolveClass(Parcel.java:2938)
CodePudding user response:
Paste the exact error you are facing, have you implemented Serializable Interface on CustomObject ?
CodePudding user response:
This link help you
https://developer.android.com/training/sharing/receive
https://developer.android.com/guide/components/aidl.html
Can you share your error logs.
CodePudding user response:
If your Custom Object implements the Serializable
(or Parcelable
) Interface, then you can bundle it in a Bundle that gets serialized or "bundled" in an Intent
.
Keep in mind it's usually a potential code smell to pass something other than a primitive or two via intents. There are multiple reasons why this may be bad. Notice that it's may be bad, not it's bad. It really depends.
In general, on any mid-sized project, it's best if you plan to simply pass some ID or needed data so that the other activity (receiver) doesn't rely directly on your custom object, but rather fetches the data from a common "repository" or even Shared View Model if you don't have a repository "pattern" of any sort.
So instead of saying Here, Have this Custom Object, you merely say: Here's the ID of the data you need.
The other issues with passing stuff via intent is that you're by default serializing on the main thread (and deserializing). Additionally, if I correctly recall, there's a size limit (1mb?) for intents so if your CustomObject is bigger, you're going to have runtime issues and/or crashes.
You would avoid pretty much all these issues if you plan ahead to decouple this from the intent/activities.