调用远程服务方法时出现Nullpointer异常

时间:2012-08-24 13:05:23

标签: android nullpointerexception android-service aidl

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

2 个答案:

答案 0 :(得分:0)

在Onclick功能中删除以下行。因为只要调用OnserviceConnected,就会初始化服务。

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

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

答案 1 :(得分:0)

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