gpt4 book ai didi

安卓 - Sphero - CalledFromWrongThreadException

转载 作者:行者123 更新时间:2023-11-29 14:39:27 24 4
gpt4 key购买 nike

我正在创建一款使用 Sphero 机器人球的游戏。当游戏开始/恢复时,它会检查配对或连接的球。如果蓝牙关闭,或者没有配对球,它会隐藏连接窗口。如果有配对但未连接的球,则转到此功能。

// If the user clicked a Sphero and it failed to connect, this event will be fired
@Override
public void onRobotConnectionFailed(Robot robot) {
Log.d("activity", "onRobotConnectionFailed");
removeConnectionView();
}

它在以下方法中崩溃。

private void removeConnectionView() {
mFrameLayout.removeView(mSpheroConnectionView);
mSpheroConnectionView = null;
}

此功能适用于彼此可能的结果。这是错误。

Thread [<14> Thread-2606] (Suspended (exception ViewRootImpl$CalledFromWrongThreadException))   
<VM does not provide monitor information>
ViewRootImpl.checkThread() line: 5031
ViewRootImpl.invalidateChildInParent(int[], Rect) line: 998
FrameLayout(ViewGroup).invalidateChild(View, Rect) line: 4358
ImageView(View).invalidate(boolean) line: 10565
ImageView(View).invalidate() line: 10520
ImageView.invalidateDrawable(Drawable) line: 202
XDrawable(Drawable).invalidateSelf() line: 382
XDrawable(Drawable).setVisible(boolean, boolean) line: 578
ImageView.onDetachedFromWindow() line: 1196
ImageView(View).dispatchDetachedFromWindow() line: 12136
FrameLayout(ViewGroup).dispatchDetachedFromWindow() line: 2824
RelativeLayout(ViewGroup).dispatchDetachedFromWindow() line: 2824
SpheroConnectionView$SpheroItemView(ViewGroup).dispatchDetachedFromWindow() line: 2824
SpheroConnectionView$SpheroListView(ViewGroup).dispatchDetachedFromWindow() line: 2824
SpheroConnectionView(ViewGroup).dispatchDetachedFromWindow() line: 2824
FrameLayout(ViewGroup).removeViewInternal(int, View) line: 3943
FrameLayout(ViewGroup).removeViewInternal(View) line: 3918
FrameLayout(ViewGroup).removeView(View) line: 3850
discgroove.removeConnectionView() line: 233
discgroove.access$4(discgroove) line: 232
discgroove$2.onRobotConnectionFailed(Robot) line: 191
SpheroConnectionView$4.onRobotConnectionFailed(Robot) line: 157
RobotProvider.update(Observable, Object) line: 570
Robot(Observable).notifyObservers(Object) line: 138
Robot.setConnected(boolean) line: 300
DeviceConnection$4.run() line: 378

最佳答案

从不是主 Looper 线程(或“UI 线程”)的线程调用 removeConnectionView 方法。 View 可以从主循环线程修改。

为了解决这个问题,有时在主 Looper 线程上实例化一个 Handler(例如在创建 View 或 Activity 时),并使用 Hander#post(Runnable) 方法调用您的方法,在您的 FrameLayout 上使用 View#post(Runnable) 方法。

第一种例子:

把它放在Activity的onCreate方法中,它总是在主Looper线程中运行。

mHandler = new Handler();

然后,当您想运行该方法时:

    handler.post(new Runnable() {
@Override
public void run() {

mFrameLayout.removeView(mSpheroConnectionView);
mSpheroConnectionView = null;
}
});

第二种例子:

    mFrameLayout.post(new Runnable() {
@Override
public void run() {
mFrameLayout.removeView(mSpheroConnectionView);
mSpheroConnectionView = null;
}
})

关于安卓 - Sphero - CalledFromWrongThreadException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16245924/

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