程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

跨进程发送对象在解组时抛出类未找到

发布于2021-06-12 09:26     阅读(1025)     评论(0)     点赞(3)     收藏(5)


我正在编码和 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];
        }
    };
}

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

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]

解决方案


暂无回答



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.javaheidong.com/blog/article/221775/8479e3b9799709f19561/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

3 0
收藏该文
已收藏

评论内容:(最多支持255个字符)