0

我正在尝试启动一项服务,但总是得到 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
4

2 回答 2

0

您的堆栈跟踪清楚地显示了中NullPointerException发生的事情okButton(),并且根据您发布的代码,该函数中的唯一行是

    startService(mServiceIntent);

很明显mServiceIntent,此时为 null,尽管设置为onCreate().

你有其他设置的线mServiceIntent吗?

或者你有一个你调用的构造函数okButton()?构造函数是我能想到的唯一会在之前运行的东西onCreate()。这也可以解释为什么onServiceConnected()尽管调用了bindService()in却从不运行onCreate()

于 2012-06-07T10:10:33.083 回答
0

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

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

你需要类似的东西:

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

从堆栈跟踪来看,在okButton()调用 onCreate() 方法之前,在创建 MyActivity 实例期间调用了该方法,当然那时 mService 将为空。

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

于 2012-06-07T11:11:36.827 回答