У меня есть класс Service
и основной класс Acitivity
, который должен получать трансляции от класса Service с помощью метода sendBroadcast.
Сбой при запуске метода sendBroadcast
из моего 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) {}
}
}
А вот часть моего класса 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);
}
Я получаю исключение NullPointerException при вызове метода sendBroadcast (intent).Любые советы по решению этого с благодарностью.
вот журнал:
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)