路过Parcelable当通过Messenger来远程服务找不到类解组时, [英] Class not found when unmarshalling when passing Parcelable through Messenger to remote service

查看:308
本文介绍了路过Parcelable当通过Messenger来远程服务找不到类解组时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我用它来从活动传递到远程服务的Parcelable对象。当我通过它使用的 AIDL 的界面,一切听起来不错。

I have a Parcelable object which I use to pass it from Activity to remote service. When I pass it using AIDL interface, everything sounds fine.

近日,笔者试图通过传递它的使者从活动。

Recently, I try to pass it through Messenger from Activity.

// TEST TEST TEST!
StockInfo stockInfo0 = new StockInfo(Code.newInstance("code0"), Symbol.newInstance("symbol0"));
StockInfo stockInfo1 = new StockInfo(Code.newInstance("code1"), Symbol.newInstance("symbol1"));
StockInfo stockInfo2 = new StockInfo(Code.newInstance("code2"), Symbol.newInstance("symbol2"));
List<StockInfo> stockInfos = new ArrayList<StockInfo>();
stockInfos.add(stockInfo0);
stockInfos.add(stockInfo1);
stockInfos.add(stockInfo2);
StockInfosEx stockInfosEx = new StockInfosEx(stockInfos, "abc");
msg.obj = stockInfosEx;

try {
    mService.send(msg);
} catch (RemoteException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

我收到的远程服务,以下情况例外。

I'm getting the following exception in remote service.

02-21 22:55:16.546:E /包裹(8365):类找不到的时候   解组:com.example.testonmessenger.StockInfosEx,E:   抛出java.lang.ClassNotFoundException:   com.example.testonmessenger.StockInfosEx

02-21 22:55:16.546: E/Parcel(8365): Class not found when unmarshalling: com.example.testonmessenger.StockInfosEx, e: java.lang.ClassNotFoundException: com.example.testonmessenger.StockInfosEx

我想知道,有什么可以错的?这是我的Parcelable对象。

I was wondering, what can get wrong in between? Here is my Parcelable object.

public class StockInfosEx implements Parcelable {
    public final List<StockInfo> stockInfos;
    public final String searchedString;

    public StockInfosEx(List<StockInfo> stockInfos, String searchedString) {
        this.stockInfos = stockInfos;
        this.searchedString = searchedString;
    }

    ////////////////////////////////////////////////////////////////////////////
    // Handling Parcelable nicely.

    public static final Parcelable.Creator<StockInfosEx> CREATOR = new Parcelable.Creator<StockInfosEx>() {
        public StockInfosEx createFromParcel(Parcel in) {
            return new StockInfosEx(in);
        }

        public StockInfosEx[] newArray(int size) {
            return new StockInfosEx[size];
        }
    };

    private StockInfosEx(Parcel in) {
        stockInfos = new ArrayList<StockInfo>();
        in.readTypedList(stockInfos, StockInfo.CREATOR);
        searchedString = in.readString();
    }

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

    @Override
    public void writeToParcel(Parcel parcel, int flags) {
        parcel.writeTypedList(stockInfos);
        parcel.writeString(searchedString);
    }

    // Handling Parcelable nicely.    
    ////////////////////////////////////////////////////////////////////////////        
}

要获得完整的源代码code,请从下载<一个href="https://www.dropbox.com/s/n69yuhddpb8vedz/testonmessenger.zip">https://www.dropbox.com/s/n69yuhddpb8vedz/testonmessenger.zip

To get complete source code, kindly download from https://www.dropbox.com/s/n69yuhddpb8vedz/testonmessenger.zip

推荐答案

活动

msg.obj = stockInfosEx;

远程服务

StockInfosEx stockInfosEx = (StockInfosEx)msg.obj;

可行的方法

活动

msg.getData().putParcelable("data", stockInfosEx);

远程服务

msg.getData().setClassLoader(StockInfosEx.class.getClassLoader());
StockInfosEx stockInfosEx = (StockInfosEx)msg.getData().getParcelable("data");


现在,当我读回msg.obj的文件(<一href="http://developer.android.com/reference/android/os/Message.html#obj">http://developer.android.com/reference/android/os/Message.html#obj)再次,只有我明白了什么叫用 Parcelable框架类


Now, after I read back the documentation of msg.obj (http://developer.android.com/reference/android/os/Message.html#obj) again, only I understand what it really mean by Parcelable of a framework class

要发送给收件人的任意对象。当使用Messenger来   发送邮件跨进程,这只能是非空的,如果它   包含一个框架类的Parcelable(而不是一个被执行   应用)。对于其它数据传输使用使用setData(捆绑)。

An arbitrary object to send to the recipient. When using Messenger to send the message across processes this can only be non-null if it contains a Parcelable of a framework class (not one implemented by the application). For other data transfer use setData(Bundle).

需要注意的是Parcelable对象这里不支持之前的Froyo   发布。

Note that Parcelable objects here are not supported prior to the FROYO release.

这篇关于路过Parcelable当通过Messenger来远程服务找不到类解组时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆