我在解组时收到 android.os.BadParcelableException: ClassNotFoundException。我检查了所有以前的帖子,但无法解决我的问题。
我也尝试使用bundle.setClassLoader(MyClass.class.getClassLoader());
readFromParcel(in) 和 writeToParcel()的顺序相同。
我的 RemoteService 和 ClientActivity 在两个单独的进程中运行。这是我的 logcat 和代码。
07-17 12:10:33.434 18348-18348/com.example.hemantb.messenger_client E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hemantb.messenger_client, PID: 18348
android.os.BadParcelableException: ClassNotFoundException when unmarshalling: com.example.hemantb.messenger_service.MyClass
at android.os.Parcel.readParcelableCreator(Parcel.java:2535)
at android.os.Parcel.readParcelable(Parcel.java:2461)
at android.os.Parcel.readValue(Parcel.java:2364)
at android.os.Parcel.readArrayMapInternal(Parcel.java:2717)
at android.os.BaseBundle.unparcel(BaseBundle.java:269)
at android.os.Bundle.getParcelable(Bundle.java:840)
at com.example.hemantb.messenger_client.ClientAvtivity$IncomingHandler.handleMessage(ClientAvtivity.java:90)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
远程服务.java
public class RemoteService extends Service {
MyClass Obj;
private static final String TAG = "MessengerService";
private static final int MSG_TYPE_REGISTER = 1;
private static final int MSG_TYPE_MESSAGE = 2;
private static final int MSG_TYPE_SEND_MSG = 3;
private static final String KEY = "data_key";
private static final String MY_KEY = "obj_key";
private Messenger mClientMessenger = null;
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind() : return messenger");
return mMessenger.getBinder();
}
private Messenger mMessenger = new Messenger(new IncomingHandler());
class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_TYPE_REGISTER:
mClientMessenger = msg.replyTo;
Log.d(TAG, "handleMessage() : register service");
break;
case MSG_TYPE_MESSAGE:
Bundle bundle = msg.getData();
if (null == bundle || null == bundle.getString(KEY)) {
Log.d(TAG, "handleMessage() : client send null");
String content = "who are you?";
sendMsgToClient(content);
} else {
// bundle.getString(KEY) not null!!
Log.d(TAG, "handleMessage() : client send the msg of --- " + bundle.getString(KEY));
String content = "the msg is : " + bundle.getString(KEY);
sendMsgToClient(content);
}
break;
default:
super.handleMessage(msg);
break;
}
}
}
private void sendMsgToClient(String content) {
if (null != mClientMessenger) {
Obj = new MyClass();
Obj.setAge(25);
Obj.setName("hemanta");
Bundle bundle = new Bundle();
bundle.putParcelable(MY_KEY, Obj);
Message msg = Message.obtain(null, MSG_TYPE_SEND_MSG);
msg.setData(bundle);
try {
mClientMessenger.send(msg);
} catch (RemoteException e) {
e.printStackTrace();
}
} else {
Log.w(TAG, "sendMsgToClient() : mClientMessager is null !!!");
}
}
MyClass.java 实现 Parcelable
public class MyClass implements Parcelable {
public int age;
public String name;
public MyClass() {
}
protected MyClass(Parcel in) {
this.age = in.readInt();
this.name = in.readString();
}
public static final Creator<MyClass> CREATOR = new Creator<MyClass>() {
@Override
public MyClass createFromParcel(Parcel in) {
return new MyClass(in);
}
@Override
public MyClass[] newArray(int size) {
return new MyClass[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(age);
dest.writeString(name);
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return this.age;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
ClientAvtivity.java 客户端活动
public class ClientAvtivity extends AppCompatActivity {
private static final String KEY = "data_key";
private static final int MSG_TYPE_REGISTER = 1;
private static final int MSG_TYPE_MESSAGE = 2;
private static final int MSG_TYPE_RECEIVE_MSG = 3;
private static final String MY_KEY = "obj_key";
private Button mSend;
private EditText mEditContent;
private TextView mReceiveMsg;
private Messenger mMessenger = new Messenger(new IncomingHandler());
private Messenger mServer = null;
private boolean mBound;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client_avtivity);
init();
}
private void init() {
mEditContent = (EditText) findViewById(R.id.edit_content);
mReceiveMsg = (TextView) findViewById(R.id.txt_receive);
mSend = (Button) findViewById(R.id.btn_send);
mSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mBound)
sendMsgToServer();
}
});
}
private void sendMsgToServer() {
Message message = Message.obtain(null, MSG_TYPE_MESSAGE);
String content = mEditContent.getText().toString();
if (null != content && content.trim().length() > 0) {
Bundle bundle = new Bundle();
bundle.putString(KEY, content);
message.setData(bundle);
}
try {
mServer.send(message);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
protected void onStart() {
super.onStart();
Intent intent = new Intent("com.example.hemantb.messenger");
intent.setPackage("com.example.hemantb.messenger_service");
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
private class IncomingHandler extends Handler{
MyClass inObj = new MyClass();
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_TYPE_RECEIVE_MSG:
Bundle bundle = msg.getData();
Log.d("IncomingHandler", "bundle.getdata" +bundle );
bundle.setClassLoader(MyClass.class.getClassLoader());
Log.d("IncomingHandler", "getClassLoader() " +MyClass.class.getClassLoader() );
inObj = (MyClass) bundle.getParcelable(MY_KEY);
if (null != bundle) {
// String content = bundle.getString(KEY);
mReceiveMsg.setText(inObj.getName());
}
break;
default:
super.handleMessage(msg);
break;
}
}
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mBound = false;
mServer = null;
Log.d("Client Activity", "onServiceDisconnected() : disconnected");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d("Client Activity", "onServiceConnected() : connected");
mBound = true;
mServer = new Messenger(service);
Message msg = Message.obtain(null, MSG_TYPE_REGISTER);
msg.replyTo = mMessenger;
try {
mServer.send(msg);
} catch (RemoteException e) {
Log.e("Client Activity", "onServiceConnected() : err is " + e.toString());
}
}
};
@Override
protected void onDestroy() {
super.onDestroy();
if (mBound) {
mBound = false;
unbindService(mConnection);
}
}