无法在Android

时间:2016-01-05 09:09:26

标签: android aidl

第一次使用AIDL。我想测试一下。 以下是我的代码:

MainActivity:

public class MainActivity extends AppCompatActivity {

private static final String TAG = "CalculateClient";

private Button btnCalculate;

private EditText etNum1;

private EditText etNum2;

private TextView tvResult;

private CalculateInterface mService;

private ServiceConnection mServiceConnection = new ServiceConnection() {

    @Override
    public void onServiceDisconnected(ComponentName name) {
        // TODO Auto-generated method stub
        logE("disconnect service");
        mService = null;
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        // TODO Auto-generated method stub
        logE("connect service");
        mService = CalculateInterface.Stub.asInterface(service);
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Bundle args = new Bundle();
    Intent intent = new Intent("com.example.calculate.CalculateService");
    intent.putExtras(args);
    bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);

    etNum1 = (EditText) findViewById(R.id.editText);
    etNum2 = (EditText) findViewById(R.id.editText2);

    tvResult = (TextView) findViewById(R.id.textView);

    btnCalculate = (Button) findViewById(R.id.button);

    btnCalculate.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            logE("Start..");
            try {
                double num1 = Double.parseDouble(etNum1.getText().toString());
                double num2 = Double.parseDouble(etNum2.getText().toString());
                logE(Double.toString(num1));
                logE(Double.toString(num2));
                String answer = "Result:" + mService.doCalculate(num1, num2); //this line BUG
                tvResult.setTextColor(Color.BLUE);
                tvResult.setText(answer);

            } catch (RemoteException ignored) {
            }
        }
    });
}

private void logE(String str) {
    Log.e(TAG, "--------" + str + "--------");
}

}

MyTestService:

  package com.radio.miao.aidltest;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class MyTestService扩展Service {

private static final String TAG = "CalculateService";
private final CalculateInterface.Stub mBinder = new CalculateInterface.Stub() {

    @Override
    public double doCalculate(double a, double b) throws RemoteException {
        // TODO Auto-generated method stub
        Log.e("Calculate", "remote");

        return b + a;
    }
};

private static void logE(String str) {
    Log.e(TAG, "--------" + str + "--------");
}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    logE("onBind()");
    return mBinder;
}

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    logE("onCreate()");
    super.onCreate();
}

@Override
public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    logE("onStart()");
    super.onStart(intent, startId);
}

@Override
public boolean onUnbind(Intent intent) {
    // TODO Auto-generated method stub
    logE("onUnbind()");
    return super.onUnbind(intent);
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    logE("onDestroy()");
    super.onDestroy();
}

}

<service android:name="com.radio.miao.aidltest.MyTestService">
        <intent-filter>
            <action android:name="com.radio.miao.aidltest.MyTestService" />
        </intent-filter>

package com.radio.miao.aidltest;

// Declare any non-default types here with import statements

interface CalculateInterface {
   double doCalculate(double a, double b);
 }

错误是 致命异议:主要                                                                          显示java.lang.NullPointerException                                                                              在com.radio.miao.aidltest.MainActivity $ 2.onClick(MainActivity.java:79)

1 个答案:

答案 0 :(得分:0)

以下行为无效服务创建意图:

Intent intent = new Intent(“com.example.calculate.CalculateService”);

正确应该是:

Intent intent = new Intent(this,MyTestService.class);