《AIDL》 客户端 连接服务端时报错:has no zero argument constructor
发布时间:2018-02-01 来源:查字典编辑
摘要:服务端服务:publicclassAddAIDLServiseextendsService{@Nullable@Overridepublic...
服务端服务:
public class AddAIDLServise extends Service{ @Nullable @Override public IBinder onBind(Intent intent) { return getMyBinder; } private final GetMyBinder.Stub getMyBinder = new GetMyBinder.Stub(){ @Override public SetNumber getMyBinder() throws RemoteException { if(setNumber == null){ setNumber = new SetNumber.Stub(){ @Override public void add() throws RemoteException { addNumber(); } @Override public void sub() throws RemoteException { subNumber(); } }; } return setNumber; } }; }
服务端连接:
Intent intent = new Intent(); intent.setClassName("com.aidl.liaoweiyi.myaidl","com.aidl.liaoweiyi.myaidl.MainActivity$AddAIDLServise"); bindService(intent, sc, Context.BIND_AUTO_CREATE); private ServiceConnection sc = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { getMyBinder = GetMyBinder.Stub.asInterface(service); try { setNumber = getMyBinder.getMyBinder(); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName name) { } };
连接时异常:java.lang.Class< com.aidl.liaoweiyi.myaidl.MainActivity$AddAIDLServise > has no zero argument constructor
回复讨论(解决方案)
内部的四大组件需要声明为静态