NullPointerException异常在sendBroadcast()从服务到活动 [英] NullPointerException at sendBroadcast() from Service to Activity

查看:718
本文介绍了NullPointerException异常在sendBroadcast()从服务到活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务类,哪些是应该从服务类接收广播与主活性的研究类方法sendBroadcast。

其上运行的方法时崩溃 sendBroadcast 从我服务类。

这是我的服务类的一部分( EDITED

 公共静态最终诠释STATE_CONNECTING = 1;    公共静态最后弦乐BT_CONNECTING =com.android.mypackage.action.BTService.BT_CONNECTING;    私人最终的IBinder mBinder =新LocalBinder();    公共类LocalBinder扩展宾德(){
            BTService的getService(){
                    返回BTService.this;
            }
    }    @覆盖
    公众的IBinder onBind(意向意图){
            返回mBinder;
    }    公共同步无效的setState(INT状态){
            mState =状态;
            如果(状态== STATE_CONNECTING){
                    意图myIntent =新意图(BT_CONNECTING);
                    尝试{
                            sendBroadcast(myIntent);
                    }
                    赶上(NullPointerException异常五){}
            }
    }

这是应该接收广播意图我Activity类的一部分( EDITED

 私人广播接收器接收器=新的广播接收器(){
            @覆盖
            公共无效的onReceive(上下文的背景下,意图意图){
                    如果(intent.getAction()。等于(BTService.BT_CONNECTING))
                            mState = STATE_CONNECTING;
                    }
            };    私人ServiceConnection mConnection =新ServiceConnection(){
            公共无效onServiceConnected(组件名的className,服务的IBinder){
                    LocalBinder粘结剂=(LocalBinder)服务;
                    为myService = binder.getService();
                    mBound = TRUE;
            }            公共无效onServiceDisconnected(组件名称为arg0){
                    mBound = FALSE;
            }
    };    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
            意向意图=新意图(这一点,BTService.class);
            myService.bindService(意向,mConnection,Context.BIND_AUTO_CREATE);            IntentFilter的过滤器=新的IntentFilter(BTService.BTConnecting);
            registerReceiver(接收器,过滤器);
    }

调用sendBroadcast(意向)方法时,我得到NullPointerException异常。解决这个任何提示大大AP preciated。

下面是日志:

 致命异常:主要
    了java.lang.RuntimeException:无法启动活动ComponentInfo {com.android.mypackage / com.android.mypackage.MyActivity}:显示java.lang.NullPointerException
    在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
    在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
    在android.app.ActivityThread.access $ 2300(ActivityThread.java:135)
    在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2136)
    在android.os.Handler.dispatchMessage(Handler.java:99)
    在android.os.Looper.loop(Looper.java:144)
    在android.app.ActivityThread.main(ActivityThread.java:4937)
    在java.lang.reflect.Method.invokeNative(本机方法)
    在java.lang.reflect.Method.invoke(Method.java:521)
    在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    在dalvik.system.NativeStart.main(本机方法)
    显示java.lang.NullPointerException:产生的原因
    在android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:271)
    在com.android.mypackage.BTService.setState(BTService.java:68)
    在com.android.mypackage.BTService.connect(BTService.java:90)
    在com.android.mypackage.MyActivity.onCreate(MyActivity.java:78)
    在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
    在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)


解决方案

从你的堆栈跟踪,看来你有某种直接引用您的 BTService 服务。既然你砍你的的onCreate()总之我不能肯定你是怎么做的,但我会采取一种猜测。

你有没有你的活动内直接实例化服务(使用新BTService())?如果是这样,那么你收到此错误的原因是因为你的服务没有绑定到它的上下文。你必须让Android的为您创建的服务,通过调用 startService() bindService()

I have a Service class and an main Acitivity class which is supposed to receive broadcasts from the Service class with method sendBroadcast.

It crashes when running the method sendBroadcast from my Service class.

here is part of my Service class (EDITED):

    public static final int STATE_CONNECTING = 1;

    public static final String BT_CONNECTING = "com.android.mypackage.action.BTService.BT_CONNECTING";

    private final IBinder mBinder = new LocalBinder();

    public class LocalBinder extends Binder() {
            BTService getService() {
                    return BTService.this;
            }
    }

    @Override
    public IBinder onBind(Intent intent) {
            return mBinder;
    }

    public synchronized void setState(int state) {
            mState = state;
            if ( state == STATE_CONNECTING ) {
                    Intent myIntent = new Intent(BT_CONNECTING);
                    try {
                            sendBroadcast(myIntent);
                    }
                    catch (NullPointerException e) {}
            }
    }

And here is part of my Activity class which is supposed to receive the broadcasted intents (EDITED):

    private BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                    if(intent.getAction().equals(BTService.BT_CONNECTING))
                            mState = STATE_CONNECTING;
                    }
            };

    private ServiceConnection mConnection = new ServiceConnection() {
            public void onServiceConnected(ComponentName className, IBinder service) {
                    LocalBinder binder = (LocalBinder) service;
                    myService = binder.getService();
                    mBound = true;
            }

            public void onServiceDisconnected(ComponentName arg0) {
                    mBound = false;
            }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
            Intent intent = new Intent(this, BTService.class);
            myService.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

            IntentFilter filter = new IntentFilter(BTService.BTConnecting);
            registerReceiver(receiver, filter);
    }

I get NullPointerException when calling sendBroadcast(intent) method. Any tips on solving this is greatly appreciated.

here is the log:

    FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.mypackage/com.android.mypackage.MyActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
    at android.app.ActivityThread.access$2300(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:144)
    at android.app.ActivityThread.main(ActivityThread.java:4937)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
    at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:271)
    at com.android.mypackage.BTService.setState(BTService.java:68)
    at com.android.mypackage.BTService.connect(BTService.java:90)
    at com.android.mypackage.MyActivity.onCreate(MyActivity.java:78)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)

解决方案

From your stack trace, it appears you are somehow directly referencing your BTService service. Since you cut your onCreate() short I can't be certain how you are doing it, but I will take a guess.

Did you instantiate this service directly inside of your activity (use new BTService())? If so, then the reason you are getting this error is because your Service has no context bound to it. You must let Android create your service for you by calling startService() or bindService().

这篇关于NullPointerException异常在sendBroadcast()从服务到活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆