Android服务始终为null

时间:2012-06-06 23:53:04

标签: java android android-service

我试图开始服务,但总是得到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);
}

结果:NullPointerException!

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" />

logcat的:

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

2 个答案:

答案 0 :(得分:0)

您的堆栈跟踪清楚地显示了NullPointerException中发生的okButton(),根据您发布的代码,该功能中唯一的一行是

    startService(mServiceIntent);

所以尽管在mServiceIntent中设置了onCreate()mServiceIntent仍为空。

您是否有其他行设置okButton()

或者你有一个构建者,你可以在其中调用onCreate()?构造函数是我能想到的唯一能在onServiceConnected()之前运行的东西。这也可以解释为什么bindService()永远不会在onCreate()中调用{{1}}的情况下运行。

答案 1 :(得分:0)

此代码不是正确的Java语法,因为方法定义没有返回类型:

private okButton(View v) {
    startService(mServiceIntent);
}

您需要以下内容:

private void okButton(View v) {
    startService(mServiceIntent);
}

我从堆栈跟踪中看到,在调用onCreate()方法之前,在创建MyActivity实例期间调用okButton()方法,当然mService将为null。

发布更多代码或更正您发布的代码,使其与您正在使用的代码实际匹配。