简体   繁体   English

Android服务始终为null

[英]Android service always null

I'm trying to start a service, but always getting NPE. 我正在尝试启动一项服务,但始终获得NPE。 What could I be doing wrong? 我可能做错了什么? Even the onServiceConnected() method does not get called, why ever? 甚至onServiceConnected()方法也不会被调用,为什么呢?

    @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! 结果: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: 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 您的堆栈跟踪清楚地显示了okButton()发生的NullPointerException ,根据您发布的代码,该函数中的唯一一行是

    startService(mServiceIntent);

So clearly mServiceIntent is null at this point, despite being set in onCreate() . 因此很明显,尽管已在onCreate()设置了mServiceIntent ,但mServiceIntent还是null。

Do you have any other lines where you set mServiceIntent ? 您还有其他设置mServiceIntent吗?

Or do you have a constructor in which you call okButton() ? 还是您有一个在其中调用okButton()的构造函数? A constructor is the only thing I can think of that would run before onCreate() . 我唯一想到的是构造函数将在onCreate()之前运行。 That would also explain why onServiceConnected() is never run despite the call to bindService() in onCreate() . 这也可以解释为什么即使在onCreate()调用onServiceConnected()也永远不会运行bindService() onCreate()

This code is not proper Java syntax, as the method definition has no return type : 此代码不是正确的Java语法,因为方法定义没有返回类型:

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. 在我看来,从该堆栈跟踪okButton()方法是在onCreate()方法之前创建MyActivity实例过程中被称为叫,当然在那个时候MSERVICE将是无效的。

Post more code or correct the code you've posted so that it actually matches what you are using. 发布更多代码或更正您发布的代码,以使其实际上与您使用的代码匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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