使用.aidl接口从远程服务返回枚举

时间:2014-02-25 18:19:17

标签: android service aidl

我正在使用.aidl界面在Android中创建远程服务。我想/需要使用枚举值。

这是我的对象类:

package com.example.remoteservice;



import java.util.Date;

enum State {Located, Missing,Failure}; 

public class PositionObject {


    State Status;


    public PositionObject(
            State status) {
        super();

        this.Status = status;

    }

    public PositionObject() {
    }


    public int getStatus() {
        return Status.ordinal();
    }

    public void setStatus(State status) {
        this.Status = status;
    }




}

这个类是远程服务的功能:

    private final IRemoteService.Stub mBinder = new Stub() {



        @Override
        public int getstatus() throws RemoteException {
            return capture.remote.getStatus();
        }


}

这是.aidl接口和.aidl.java:

package com.example.remoteservice;

// service interface
interface IRemoteService {
    //sample method
    int getstatus();


}

@Override public int getstatus() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
int _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_getstatus, _data, _reply, 0);
_reply.readException();   THIS IS THE LINE WICH PRODUCES ERROR
_result = _reply.readInt();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}

当我在第二个项目中调用这个函数时,例如log.i crash:

Log.i("telo","telo"+String.valueOf(mRemoteService.getstatus()));

logacat:

02-25 19:44:03.818: E/AndroidRuntime(21900): FATAL EXCEPTION: serviceThread
02-25 19:44:03.818: E/AndroidRuntime(21900): Process: com.example.grafica, PID: 21900
02-25 19:44:03.818: E/AndroidRuntime(21900): java.lang.NullPointerException
02-25 19:44:03.818: E/AndroidRuntime(21900):    at android.os.Parcel.readException(Parcel.java:1471)
02-25 19:44:03.818: E/AndroidRuntime(21900):    at android.os.Parcel.readException(Parcel.java:1419)
02-25 19:44:03.818: E/AndroidRuntime(21900):    at com.example.remoteservice.IRemoteService$Stub$Proxy.getstatus(IRemoteService.java:274)
02-25 19:44:03.818: E/AndroidRuntime(21900):    at com.example.grafica.MainActivity$1$1.run(MainActivity.java:300)
02-25 19:44:03.818: E/AndroidRuntime(21900):    at java.lang.Thread.run(Thread.java:841)

会发生什么?我觉得没关系。没有?

感谢

0 个答案:

没有答案