问题
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)
回答1:
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()
.
回答2:
Try changing your sendBroadcast
call to: context.sendBroadcast(intent);
回答3:
Try to add myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
来源:https://stackoverflow.com/questions/9826825/nullpointerexception-at-sendbroadcast-from-service-to-activity