0

我正在尝试在我的 Activity 中调用远程服务的 add 方法。在 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()");
  }
  }
4

2 回答 2

0

我认为直接使用 ServicConnection 而不是创建它的类更好。请参考这篇希望对 developer.samsung.com/android/technical-docs/Services-with-AIDL-in-Android 有所帮助

于 2014-01-09T10:24:40.757 回答
0

在 Onclick 函数中删除下面的行。因为只要 OnserviceConnected 被调用,你正在初始化服务。

  service = IAdditionService.Stub.asInterface((IBinder) boundService);

您尚未在任何地方初始化 boundservice 尚未分配它

于 2012-08-24T15:04:17.087 回答