我正在尝试启动一项服务,但总是得到 NPE。我可能做错了什么?甚至该onServiceConnected()
方法也没有被调用,为什么?
@Override
protected void onCreate(Bundle savedInstanceState) {
mServiceIntent = new Intent(this, MyService.class);
bindService(mServiceIntent, mConnection, Context.BIND_AUTO_CREATE);
//other stuff
}
用法:
private okButton(View v) {
startService(mServiceIntent);
}
结果:空指针异常!
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
Log.v("service", "gets never executed!");
mDownloadService = ((LocalBinder<MyService>) service).getService();
}
@Override
public void onServiceDisconnected(ComponentName className) {
}
};
}
public class LocalBinder<S> extends Binder {
private String TAG = "LocalBinder";
private WeakReference<S> mService;
public LocalBinder(S service){
mService = new WeakReference<S>(service);
}
public S getService() {
return mService.get();
}
}
当然,我也有清单中的服务:
<service android:enabled="true" android:name=".service.MyService" />
日志猫:
06-06 23:48:20.411: W/dalvikvm(592): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
06-06 23:48:20.461: E/AndroidRuntime(592): FATAL EXCEPTION: main
06-06 23:48:20.461: E/AndroidRuntime(592): java.lang.RuntimeException: Unable to start activity ComponentInfo{MyActivity}: java.lang.NullPointerException
06-06 23:48:20.461: E/AndroidRuntime(592): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-06 23:48:20.461: E/AndroidRuntime(592): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-06 23:48:20.461: E/AndroidRuntime(592): at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-06 23:48:20.461: E/AndroidRuntime(592): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-06 23:48:20.461: E/AndroidRuntime(592): at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 23:48:20.461: E/AndroidRuntime(592): at android.os.Looper.loop(Looper.java:137)
06-06 23:48:20.461: E/AndroidRuntime(592): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-06 23:48:20.461: E/AndroidRuntime(592): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 23:48:20.461: E/AndroidRuntime(592): at java.lang.reflect.Method.invoke(Method.java:511)
06-06 23:48:20.461: E/AndroidRuntime(592): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-06 23:48:20.461: E/AndroidRuntime(592): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-06 23:48:20.461: E/AndroidRuntime(592): at dalvik.system.NativeStart.main(Native Method)
06-06 23:48:20.461: E/AndroidRuntime(592): Caused by: java.lang.NullPointerException
06-06 23:48:20.461: E/AndroidRuntime(592): at MyActivity.okButton(MyActivity.java:217)
06-06 23:48:20.461: E/AndroidRuntime(592): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1939)
06-06 23:48:20.461: E/AndroidRuntime(592): ... 11 more
意图似乎没问题:
Log.v("service", String.valueOf(mServiceIntent == null)); //false