Note that there are some explanatory texts on larger screens.

plurals
  1. PONullPointerException at sendBroadcast() from Service to Activity
    primarykey
    data
    text
    <p>I have a <code>Service</code> class and an main <code>Acitivity</code> class which is supposed to receive broadcasts from the Service class with method sendBroadcast.</p> <p>It crashes when running the method <code>sendBroadcast</code> from my <code>Service</code> class.</p> <p>here is part of my Service class (<strong>EDITED</strong>):</p> <pre><code> 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) {} } } </code></pre> <p>And here is part of my Activity class which is supposed to receive the broadcasted intents (<strong>EDITED</strong>):</p> <pre><code> 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); } </code></pre> <p>I get NullPointerException when calling sendBroadcast(intent) method. Any tips on solving this is greatly appreciated.</p> <p>here is the log:</p> <pre><code> 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) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload