在OnCreate()方法中绑定服务时出现NullPointerException

时间:2013-10-07 10:24:57

标签: java android service

我想将服务绑定到一个活动,以便从服务中读取从手机中检索到的一些数据并将其显示在TextView中。

请参考下面的代码:Facts()方法将使用调用mService方法后不应为null的StartServiceFacts引用,但它为null。

但是,当我在一个单独的按钮中调用tFacts.setText(Onfacts)方法时,它可以正常工作。那么为什么我不能在onCreate方法中绑定到服务,为什么我可以实现这一点以避免使用任何额外的按钮?

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.handsetdetailsactivity);
    mConnection = new ConnectionClass();
    tFacts = (TextView) findViewById(R.id.tHandsetDtails);
    Intent intent = new Intent(HandsetDetailsActivity.this, 
        ServiceDeviceFacts.class);
    startService(intent);
    StartServiceFacts();
    tFacts.setText(Facts());
}

void StartServiceFacts() {
    Intent intent = new Intent(this, ServiceDeviceFacts.class);
    try {
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    } catch (Exception ex) {
        Log.e("Exception in the service", ex.toString());
    }
    Log.e("mBound value", mBound + "");
}

private class ConnectionClass implements ServiceConnection {

    @Override
    public void onServiceConnected(ComponentName className, IBinder service) {
        ServiceBinder binder = (ServiceBinder) service;
        mService = binder.getService();
        mBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        mBound = false;
    }
};

记录cat消息错误:

10-07 11:41:28.007: E/AndroidRuntime(24964): FATAL EXCEPTION: main
10-07 11:41:28.007: E/AndroidRuntime(24964): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testma/com.testma.activities.HandsetDetailsActivity}: java.lang.NullPointerException
10-07 11:41:28.007: E/AndroidRuntime(24964):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
10-07 11:41:28.007: E/AndroidRuntime(24964):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
10-07 11:41:28.007: E/AndroidRuntime(24964):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-07 11:41:28.007: E/AndroidRuntime(24964):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
10-07 11:41:28.007: E/AndroidRuntime(24964):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-07 11:41:28.007: E/AndroidRuntime(24964):    at android.os.Looper.loop(Looper.java:130)
10-07 11:41:28.007: E/AndroidRuntime(24964):    at android.app.ActivityThread.main(ActivityThread.java:3687)
10-07 11:41:28.007: E/AndroidRuntime(24964):    at java.lang.reflect.Method.invokeNative(Native Method)
10-07 11:41:28.007: E/AndroidRuntime(24964):    at java.lang.reflect.Method.invoke(Method.java:507)
10-07 11:41:28.007: E/AndroidRuntime(24964):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
10-07 11:41:28.007: E/AndroidRuntime(24964):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
10-07 11:41:28.007: E/AndroidRuntime(24964):    at dalvik.system.NativeStart.main(Native Method)
10-07 11:41:28.007: E/AndroidRuntime(24964): Caused by: java.lang.NullPointerException
10-07 11:41:28.007: E/AndroidRuntime(24964):    at com.testma.activities.HandsetDetailsActivity.Facts(HandsetDetailsActivity.java:81)
10-07 11:41:28.007: E/AndroidRuntime(24964):    at com.testma.activities.HandsetDetailsActivity.onCreate(HandsetDetailsActivity.java:36)
10-07 11:41:28.007: E/AndroidRuntime(24964):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-07 11:41:28.007: E/AndroidRuntime(24964):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
10-07 11:41:28.007: E/AndroidRuntime(24964):    ... 11 more

1 个答案:

答案 0 :(得分:0)

绑定到ServiceAsynchronous操作。这就是为什么在调用StartServiceFacts()后无法立即执行面向服务的操作。

您必须等到ConnectionClass中调用 onServiceConnected 方法,该方法确认其与Activity绑定。

关于你的按钮之谜!它在按钮按下时工作正常的原因是因为在按钮的 onClick 被引发之前服务已经被绑定。