Server android.os.BadParcelableException:使用messenger在服务器和客户端之间解组时发生ClassNotFoundException

Server android.os.BadParcelableException:使用messenger在服务器和客户端之间解组时发生ClassNotFoundException,server,client,classnotfoundexception,messenger,badparcelableexception,Server,Client,Classnotfoundexception,Messenger,Badparcelableexception,我在解组时得到android.os.BadParcelableException:ClassNotFoundException。我检查了所有以前的帖子,但没能解决我的问题 我还尝试使用bundle.setClassLoaderMyClass.class.getClassLoader readFromParcelin和writeToParcel的顺序相同 我的RemoteService和ClientActivity在两个单独的进程中运行。 这是我的日志和代码 07-17 12:10:33.4

我在解组时得到android.os.BadParcelableException:ClassNotFoundException。我检查了所有以前的帖子,但没能解决我的问题

我还尝试使用bundle.setClassLoaderMyClass.class.getClassLoader

readFromParcelin和writeToParcel的顺序相同

我的RemoteService和ClientActivity在两个单独的进程中运行。 这是我的日志和代码

   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)
RemoteService.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 !!!");
    }
}
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;
}

我得到了我问题的答案。。。由于我的客户端和服务器在两个独立的应用程序中运行,因此我的parcelable类必须位于客户端和服务器中的同一个包中。

嘿,伙计,有相同的问题..尝试此操作但尚未工作。有些设备还可以,但大多数都不行。我也遇到过同样的情况,但通过你的回答,我知道发生了什么。分离的进程应该使用相同的包看到相同的可调整的类,这是有道理的。
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);
    }
}