开发者社区> 问答> 正文

关于安卓开发的问题

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的值了吗?这里应该如何去改?

展开
收起
爵霸 2016-03-25 10:12:41 1852 0
1 条回答
写回答
取消 提交回答
  • 你的count首先为什么用String类型,并且我从你的代码中并未看到count被赋值,唯一 一个地方就是你的getcount()把这个值return出去了,都没赋值,能不空指针吗?

    2019-07-17 19:13:37
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Re: 移动开发的未来 立即下载
APICloud服务CAF开发者 立即下载
ios开发者必读咨讯 立即下载