我已经为IPC开发了一个演示程序,并使用有界服务来处理它....但是在使用复杂数据结构时面临一些问题(即在parceable中可以分辨)。
我有这样的数据结构......
ObjectX.java
public class ObjectX implements Parcelable {
private Integer code;
private OtherEntity entity;
public ObjectX(Integer _code, OtherEntity _entity) {
code = _code;
entity = _entity;
}
public ObjectX(Parcel source) {
readfromParcel(source);
}
private void readfromParcel(Parcel source) {
code = source.readInt();
entity = (OtherEntity) source.readParcelable(OtherEntity.class
.getClassLoader());
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(code);
dest.writeParcelable(entity, flags);
}
public static final Parcelable.Creator<ObjectX> CREATOR = new Parcelable.Creator<ObjectX>() {
@Override
public ObjectX[] newArray(int size) {
return new ObjectX[size];
}
@Override
public ObjectX createFromParcel(Parcel source) {
return new ObjectX(source);
}
};
}
OtherEntity.java
public class OtherEntity implements Parcelable {
private Integer m_code;
public OtherEntity(Parcel source) {
m_code = source.readInt();
}
public OtherEntity(Integer code) {
m_code = code;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(m_code);
}
public static final Parcelable.Creator<OtherEntity> CREATOR = new Parcelable.Creator<OtherEntity>() {
@Override
public OtherEntity[] newArray(int size) {
return new OtherEntity[size];
}
@Override
public OtherEntity createFromParcel(Parcel source) {
return new OtherEntity(source);
}
};
}
我已经在课程中实现了Parcelable。
我的aidl文件是:
ObjectX.aidl:
package com.example.demo.A;
parcelable ObjectX;
IMyService.aidl:
package com.example.demo.A;
import com.example.demo.A.ObjectX ;
interface IMyService {
void setOrderData(in ObjectX data);
}
现在在对象列表中设置数据时如下:
orderData.add(order);
以
传递mService.setOrderData(new Object(1,new OtherEntity(1));
我得到了这个例外:
05-20 15:35:28.018: E/AndroidRuntime(818): FATAL EXCEPTION: main
05-20 15:35:28.018: E/AndroidRuntime(818): java.lang.NullPointerException: expected receiver of type com.example.remoteservice.OtherEntity, but got null
05-20 15:35:28.018: E/AndroidRuntime(818): at java.lang.reflect.Field.getField(Native Method)
05-20 15:35:28.018: E/AndroidRuntime(818): at java.lang.reflect.Field.get(Field.java:269)
05-20 15:35:28.018: E/AndroidRuntime(818): at android.os.Parcel.readParcelable(Parcel.java:1955)
05-20 15:35:28.018: E/AndroidRuntime(818): at com.example.remoteservice.ObjectX.readfromParcel(ObjectX.java:58)
05-20 15:35:28.018: E/AndroidRuntime(818): at com.example.remoteservice.ObjectX.<init>(ObjectX.java:49)
05-20 15:35:28.018: E/AndroidRuntime(818): at com.example.remoteservice.ObjectX$1.createFromParcel(ObjectX.java:94)
05-20 15:35:28.018: E/AndroidRuntime(818): at com.example.remoteservice.ObjectX$1.createFromParcel(ObjectX.java:1)
05-20 15:35:28.018: E/AndroidRuntime(818): at android.os.Parcel.createTypedArrayList(Parcel.java:1616)
05-20 15:35:28.018: E/AndroidRuntime(818): at com.example.remoteservice.IRemote$Stub$Proxy.getOrderData(IRemote.java:247)
05-20 15:35:28.018: E/AndroidRuntime(818): at com.example.consumerapp3.RemoteServiceClient.onClick(RemoteServiceClient.java:95)
05-20 15:35:28.018: E/AndroidRuntime(818): at android.view.View.performClick(View.java:3480)
05-20 15:35:28.018: E/AndroidRuntime(818): at android.view.View$PerformClick.run(View.java:13983)
05-20 15:35:28.018: E/AndroidRuntime(818): at android.os.Handler.handleCallback(Handler.java:605)
05-20 15:35:28.018: E/AndroidRuntime(818): at android.os.Handler.dispatchMessage(Handler.java:92)
05-20 15:35:28.018: E/AndroidRuntime(818): at android.os.Looper.loop(Looper.java:137)
05-20 15:35:28.018: E/AndroidRuntime(818): at android.app.ActivityThread.main(ActivityThread.java:4340)
05-20 15:35:28.018: E/AndroidRuntime(818): at java.lang.reflect.Method.invokeNative(Native Method)
05-20 15:35:28.018: E/AndroidRuntime(818): at java.lang.reflect.Method.invoke(Method.java:511)
05-20 15:35:28.018: E/AndroidRuntime(818): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-20 15:35:28.018: E/AndroidRuntime(818): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-20 15:35:28.018: E/AndroidRuntime(818): at dalvik.system.NativeStart.main(Native Method)
有谁能告诉我我错过了什么?
答案 0 :(得分:0)
我猜你的构造函数public Object(Parcel source)
是一个空值作为源。
答案 1 :(得分:0)
你的代码工作正常,我假设:
您调用AIDL方法的代码:
mService.setOrderData(new Object(1,new OtherEntity(1));
表示:
mService.setOrderData(new ObjectX(1, new OtherEntity(1)));
OtherEntity.aidl :
package com.example.demo.A;
parcelable OtherEntity;
休息一切都很好。希望它有所帮助