跨进程抛出对象抛出解组时找不到类

时间:2016-07-23 06:07:26

标签: java android parcelable

我的编码和Android应用程序有2个进程,一个显然是处理所有UI事物和其他东西的主要进程,另一个进程托管处理网络(套接字)侦听和其他事情的服务。

为了在两个进程之间进行通信,我使用了Messenger方法,并且在我需要发送自定义java对象之前一直运行良好。这个java类实现了Parcelable接口,它允许Messenger跨进程边界发送java对象。

这是我要发送的java类的一个例子。

package com.locator.carlocator.models;

import android.os.Parcel;
import android.os.Parcelable;

public class Phone implements Parcelable {

    public String number;
    public String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.number);
        dest.writeString(this.name);
    }

    public Phone() {
    }

    private Phone(Parcel in) {
        this.number = in.readString();
        this.name = in.readString();
    }

    public static final Parcelable.Creator<Phone> CREATOR = new Parcelable.Creator<Phone>() {
        @Override
        public Phone createFromParcel(Parcel source) {
            return new Phone(source);
        }

        @Override
        public Phone[] newArray(int size) {
            return new Phone[size];
        }
    };
}

这就是我如何创建我要发送的对象实例,这是由收到的消息触发,然后发送到主进程。下一个代码块运行在一个服务上,该服务在不同的进程上运行,并使用内置API的Messenger IPC进行通信。

Phone phone = new Phone();
phone.setNumber("6000-0006");
phone.setName("Alex Sanchez");

data = new Bundle();
data.setClassLoader(Phone.class.getClassLoader());
data.putParcelable("key-phone", phone);

Message msg = Message.obtain(null, MSG_A_REQUEST);
msg.setData(data);
notifySubscribers(msg);   // this method perform the send message.

如何实现发送方法?

private void notifySubscribers(Message msg) {
    for(Messenger subscriber : serviceSubscribers) {
        try {
            subscriber.send(msg);
        } catch (RemoteException e) {
            Log.e(TAG, "Subscriber not available");
            e.printStackTrace();
        }
    }
}

这是堆栈跟踪的一部分:

00:53:05.382 21542-21542 E/Parcel: Class not found when unmarshalling: com.locator.carlocator.models.Phone
                                     java.lang.ClassNotFoundException: com.locator.carlocator.models.Phone
                                         at java.lang.Class.classForName(Native Method)
                                         at java.lang.Class.forName(Class.java:309)
                                         at java.lang.Class.forName(Class.java:273)
                                         at android.os.Parcel.readParcelableCreator(Parcel.java:2281)
                                         at android.os.Parcel.readParcelable(Parcel.java:2245)
                                         at android.os.Parcel.readValue(Parcel.java:2152)
                                         at android.os.Parcel.readArrayMapInternal(Parcel.java:2485)
                                         at android.os.BaseBundle.unparcel(BaseBundle.java:221)
                                         at android.os.Bundle.getParcelable(Bundle.java:755)
                                         at com.locator.carlocator.MainActivity$IncomingHandler.handleMessage(MainActivity.java:2255)
                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                         at android.os.Looper.loop(Looper.java:135)
                                         at android.app.ActivityThread.main(ActivityThread.java:5343)
                                         at java.lang.reflect.Method.invoke(Native Method)
                                         at java.lang.reflect.Method.invoke(Method.java:372)
                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:702)
                                      Caused by: java.lang.ClassNotFoundException: Didn't find class "com.locator.carlocator.models.Phone" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/vendor/lib, /system/lib]

0 个答案:

没有答案