android AIDL问题

xiaoleelee 2012-10-12 02:23:00
我自己写了一个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的,还请高手批教一下,多谢谢了!
...全文
183 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Five_Cent_Nicol 2012-10-12
  • 打赏
  • 举报
回复
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
catService = icat.Stub.asInterface(service);
}
};

你这句话执行完之后,才有catService
xiaoleelee 2012-10-12
  • 打赏
  • 举报
回复
什么时候onServiceConnected才能执行完了啊?怎样在程序里判断啊?
深耕安卓 2012-10-12
  • 打赏
  • 举报
回复
打那句log的时候,bind 还没有联上,就是onServiceConnected还没执行

80,392

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧