IntentService.java
public class Intentservice1 extends IntentService {
private String count;
private mybinder bind=new mybinder();
public class mybinder extends Binder
{
public String getcount()
{
System.out.println(count);
return count;
}
}
public Intentservice1()
{
super("Intentservice1");
System.out.println("構造方法");
}
@Override
public void onCreate()
{
super.onCreate();
System.out.println("onCreate方法");
}
@Override
public IBinder onBind(Intent intenr)
{
System.out.println("service isbind");
return bind;
}
@Override
protected void onHandleIntent(Intent arg0) {
// TODO Auto-generated method stub
}
}
MainActivity.java
private Intentservice1.mybinder binder1;
private ServiceConnection conn=new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName name,IBinder service)
{
System.out.println("service is connect");
binder1=(Intentservice1.mybinder)service;
System.out.println(binder1.getcount()); //這裡發生了NullPointerException
}
在運行的時候發生NullPointerException,請問為什麼binder1.getcount()會發生這樣的錯誤?binder1不是已經獲得返回的Intentservice1.mybinder的值了嗎?這裡應該如何去改?
你的count首先為什麼用String類型,並且我從你的代碼中並未看到count被賦值,唯一 一個地方就是你的getcount()把這個值return出去了,都沒賦值,能不空指針嗎?