gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:02:41 25 4
gpt4 key购买 nike

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

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

但是当我打电话tFacts.setText(Onfacts)时方法在一个单独的按钮中工作正常。那么为什么我无法绑定(bind)到 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;
}
};

记录猫消息错误:

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

最佳答案

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

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

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

关于java - 在 OnCreate() 方法中绑定(bind)服务时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19222358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com