Activity でリモート サービスの add メソッドを呼び出そうとしています。In Activity リモート サービス メソッドの呼び出しが原因nullpointerexception
です:
//This where i am calling service method in my activity
initService();
service.add(); //this call causing null pointer exception
///My .aidl file
interface IAdditionService {
void add();
}
//my service where add method is defined
@Override
public IBinder onBind(Intent intent) {
return new IAdditionService.Stub() {
/**
* Implementation of the add() method
*/
public void add() throws RemoteException {
Log.i(TAG,"INSIDE add method of IAdditionService.Stub() before addition ");
}
};
}
これで私を助けてください。初めてサービスを実装します。
私のコード全体:- 私の活動コード:-
public class AIDLDemo extends Activity {
private static final String TAG = "AIDLDemo";
IAdditionService service;
IBinder boundService;
AdditionServiceConnection connection;
/**
* This class represents the actual service connection. It casts the bound
* stub implementation of the service to the AIDL interface.
*/
class AdditionServiceConnection implements ServiceConnection {
public void onServiceConnected(ComponentName name, IBinder boundService) {
service = IAdditionService.Stub.asInterface((IBinder) boundService);
Log.d(AIDLDemo.TAG, "onServiceConnected() connected");
Toast.makeText(AIDLDemo.this, "Service connected", Toast.LENGTH_LONG)
.show();
}
public void onServiceDisconnected(ComponentName name) {
service = null;
Log.d(AIDLDemo.TAG, "onServiceDisconnected() disconnected");
Toast.makeText(AIDLDemo.this, "Service connected", Toast.LENGTH_LONG)
.show();
}
}
/** Binds this activity to the service. */
private void initService() {
connection = new AdditionServiceConnection();
Intent i = new Intent();
i.setClassName("com.marakana", com.marakana.AdditionService.class.getName());
boolean ret = bindService(i, connection, Context.BIND_AUTO_CREATE);
Log.d(TAG, "initService() bound with " + ret);
}
/** Unbinds this activity from the service. */
private void releaseService() {
unbindService(connection);
connection = null;
Log.d(TAG, "releaseService() unbound.");
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initService();
// Setup the UI
Button buttonCalc = (Button) findViewById(R.id.buttonCalc);
buttonCalc.setOnClickListener(new OnClickListener() {
TextView result = (TextView) findViewById(R.id.result);
EditText value1 = (EditText) findViewById(R.id.value1);
EditText value2 = (EditText) findViewById(R.id.value2);
public void onClick(View v) {
int v1, v2, res = -1;
service = IAdditionService.Stub.asInterface((IBinder) boundService);
v1 = Integer.parseInt(value1.getText().toString());
v2 = Integer.parseInt(value2.getText().toString());
try {
service.add();
} catch (RemoteException e) {
Log.d(AIDLDemo.TAG, "onClick failed with: " + e);
e.printStackTrace();
}
result.setText(new Integer(res).toString());
}
});
}
/** Called when the activity is about to be destroyed. */
@Override
protected void onDestroy() {
releaseService();
}
}
私のサービス:-
public class AdditionService extends Service {
private static final String TAG = "AdditionService";
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate()");
}
@Override
public IBinder onBind(Intent intent) {
return new IAdditionService.Stub() {
/**
* Implementation of the add() method
*/
public void add() throws RemoteException {
Log.d(TAG, String.format("AdditionService.add(%d, %d)"));
// return value1 + value2;
}
};
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy()");
}
}