gpt4 book ai didi

java - 关闭 ProgressDialog 时出错 - E/ViewRootImpl : sendUserActionEvent() mView == null

转载 作者:行者123 更新时间:2023-11-29 23:20:37 31 4
gpt4 key购买 nike

抱歉,我知道这个问题已经被问过几次了。但我已经尝试了建议的解决方案,但没有成功。

在我的 Android 应用程序中,我开始随机出现这个错误,我没有修改代码,它运行了几次都很好,现在它向我显示了这个错误:

`E/ViewRootImpl: sendUserActionEvent() mView == null`

logcat

当我调用设置了 BluetoothConnectionService 的 java 类时,就会发生这种情况。具体来说,它发生在调用关闭进度对话框的方法时。

`public ConnectedThread(BluetoothSocket mSocket) {
Log.d(TAG, "ConnectedThread: Starting");

mmBTSocket = mSocket;
InputStream mTempIn = null;
OutputStream mTempOut = null;
// dismiss the progressdialog when the connection is established.
try{
mProgressDialog.dismiss();
} catch (NullPointerException e) {
Log.e(TAG, "ConnectedThread: Couldn't dismiss progressDialogBox" + e.getMessage(), e);
}
try {
mTempIn = mmBTSocket.getInputStream();
mTempOut = mmBTSocket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "ConnectedThread: Failed to get I/O Stream: " + e.getMessage(), e);
}
mInStream = mTempIn;
mOutStream = mTempOut;
}`

在Dialog.java文件中,我认为是在这个方法中发生的。

`@Override
public void dismiss() {
if (Looper.myLooper() == mHandler.getLooper()) {
dismissDialog();
} else {
mHandler.post(mDismissAction);
}
}`

我在 stackoverflow 上找到的关于此错误的先前建议没有奏效。我尝试将以下代码添加到我的 AndroidManifest 中,但没有成功:

`       android:name=".MainActivity"
android:configChanges="keyboardHidden|orientation|screenLayout|screenSize"
android:label="@string/app_name"`

拜托,任何建议都会有所帮助,因为在这种情况发生之前我已经完成了应用程序的大部分工作,但我不明白为什么!!

最佳答案

您需要检查您的上下文是否为空。这主要发生在您指向的上下文或 View 变为空时。在调用之前尝试执行以下检查:

 if ( getContext() != null && getView != null )
{
// do your stuff here
}

您还需要在主线程上更新 View 。如果您不在主线程上,那么您应该实现处理程序来更新您的 UI 组件。访问以下链接了解详情:

https://developer.android.com/training/multiple-threads/communicate-ui

执行以下操作:

 Handler handler = new Handler(); // write in onCreate function

handler.post(new Runnable() {
@Override
public void run()
{
// Update your UI components here
}
});

关于java - 关闭 ProgressDialog 时出错 - E/ViewRootImpl : sendUserActionEvent() mView == null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54373607/

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