【问题标题】:NullPointerException at sendBroadcast() from Service to Activity从服务到活动的 sendBroadcast() 处的 NullPointerException
【发布时间】:2012-03-22 16:48:45
【问题描述】:

我有一个Service 类和一个主Acitivity 类,它应该使用方法sendBroadcast 从Service 类接收广播。

从我的 Service 类运行方法 sendBroadcast 时崩溃。

这是我的服务类的一部分(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) {}
            }
    }

这是我的 Activity 类的一部分,它应该接收广播的意图(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);
    }

调用 sendBroadcast(intent) 方法时,我得到 NullPointerException。非常感谢任何解决此问题的提示。

这是日志:

    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)

【问题讨论】:

  • 从 LogCat 发布完整的堆栈跟踪。
  • 好的。我现在使用 LogCat 的堆栈跟踪编辑了主帖子。
  • 当您捕捉到这个异常时,您在 logcat 中看到 NPE 没有任何意义。你确定这是执行的代码吗?
  • 这是在没有 try catch 语句的情况下被捕获的。

标签: android broadcastreceiver


【解决方案1】:

尝试将您的 sendBroadcast 呼叫更改为:context.sendBroadcast(intent);

【讨论】:

  • 好的。那是行不通的;无法从 Context 类型对非静态方法 sendBroadcast(Intent) 进行静态引用。
  • 试试:Context.sendBroadcast(new Intent (context, name.class));
  • 这适用于我使用MyContext = getApplicationContext(); 然后MyContext.sendBroadcast(intent);
【解决方案2】:

从您的堆栈跟踪来看,您似乎以某种方式直接引用了您的BTService 服务。既然你把你的onCreate()剪短了,我不能确定你是怎么做的,但我会猜测一下。

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

【讨论】:

  • 这有帮助!它将堆栈跟踪中的错误数量减少到: 引起:java.lang.NullPointerException at com.android.mypackage.MyActivity.onCreate(MyActivity.java:78) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
  • MyActivity.java 中的第 78 行是什么?不管它是什么,它都会导致 NullPointerException。
  • 就是这一行:myService.bindService(intent, mConnection, Context.BIND_AUTO_CREATE); ...我在主帖中编辑了更多代码,因此可能更容易看到。
  • 不要使用 myService.bindService()。 myService 为空。只需调用 bindService() 因为您已经是 Context 对象。另外,请记住 myService 直到某个未知时间才会设置,因此请确保在 mBound 为 true 之前不要对其调用任何内容。
【解决方案3】:

尝试添加myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多