简体   繁体   中英

Android service always null

I'm trying to start a service, but always getting NPE. What could I be doing wrong? Even the onServiceConnected() method does not get called, why ever?

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       mServiceIntent = new Intent(this, MyService.class);
       bindService(mServiceIntent, mConnection, Context.BIND_AUTO_CREATE);
       //other stuff
    }

usage:

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

result: 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();
    }
}

Of course I also have the service in manifest:

<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

The intent seems to be ok:

Log.v("service", String.valueOf(mServiceIntent == null)); //false

Your stack trace clearly shows the NullPointerException happening in okButton() , and according to the code you posted, the only line in that function is

    startService(mServiceIntent);

So clearly mServiceIntent is null at this point, despite being set in onCreate() .

Do you have any other lines where you set mServiceIntent ?

Or do you have a constructor in which you call okButton() ? A constructor is the only thing I can think of that would run before onCreate() . That would also explain why onServiceConnected() is never run despite the call to bindService() in onCreate() .

This code is not proper Java syntax, as the method definition has no return type :

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

You need something like:

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

It looks to me from the stack trace that the okButton() method is being called during the creation of the MyActivity instance before the onCreate() method is called and of course at that time mService will be null.

Post more code or correct the code you've posted so that it actually matches what you are using.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM