我自己写了一个AIDL的demo,但是运行的时候总是出现空指针异常,请高手指教,多谢!新建了一个应用,里面没有activity,只有一个service,如下:
package com.lee;import java.util.Timer;
import java.util.TimerTask;import com.lee.icat.Stub;import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;public class AidlService extends Service { private CatBinder catBinder;
Timer timer = new Timer();
String[] colors = new String[]{"红色","黄色","黑色"};
double[] weights = new double[] {2.3, 3.1, 1.58};
private String color;
private double weight;

public class CatBinder extends Stub{ @Override
public String getColor() throws RemoteException {
return color;
} @Override
public double getWeight() throws RemoteException {
return weight;
}
}

@Override
public void onCreate() {
super.onCreate();
catBinder = new CatBinder();
timer.schedule(new TimerTask() {

@Override
public void run() {
int rand = (int)(Math.random() * 3);
color = colors[rand];
weight = weights[rand];
Log.i("lcz", String.valueOf(rand));
}
}, 0, 800);
} @Override
public IBinder onBind(Intent intent) {
return catBinder;
} @Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
timer.cancel();

}和这个service在同一个包下有一个icat.aidl文件,内容如下:
package com.lee;
interface icat{
String getColor();
double getWeight();
}manifest.xml的配置如下:<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.lee"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <service android:name=".AidlService" >
            <intent-filter>
<action android:name="com.lee.action.AIDL_SERVICE"/>
            </intent-filter>
        </service>
    </application>
</manifest>然后,我启动了这个应用,但是在设备的应用程序列表中没有找到这个应用???然后我又新建了一个应用,而且把上面的那个aidl文件拷到了这个应用的同样包名的包下,这个应用里只有一个activity,如下:package com.client;import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;import com.lee.icat;public class ClientActivity extends Activity { private icat catService;

private ServiceConnection connection = new ServiceConnection() {

@Override
public void onServiceDisconnected(ComponentName name) {
catService = null;
}

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
catService = icat.Stub.asInterface(service);
}
};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent intent = new Intent();
intent.setAction("com.lee.action.AIDL_SERVICE");
bindService(intent, connection, Context.BIND_AUTO_CREATE);
try {
Log.i("lcz", catService.getColor() + catService.getWeight());
} catch (RemoteException e) {
e.printStackTrace();
}
    } @Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
this.unbindService(connection);
}
    
}然后我运行这个应用,就提示,空指针异常,我跟了一下,Log.i("lcz", catService.getColor() + catService.getWeight());出错,catService是null的,还请高手批教一下,多谢谢了!