- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
thread = new Thread() {
public void run() {
super.run();
System.out.println("run:" + Thread.currentThread().getName());
Looper.prepare();
handler = new Handler();
Looper.loop();
};
};
thread.start();
然后
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(MActivity.this,Thread.currentThread().getName(),0).show();
}
});
代码运行正确。
但是 toast 显示:“Thread-217”
这意味着 toast 从非 uithread 显示。
为什么?
我很抱歉。我知道答案。 Toast 是一个特殊的 UI 元素。它可以从任何线程显示。但是其他 UI 元素,例如 Button TextView 只能在 UI 线程中被触及。所以,我的代码运行正确,但是当您将 toast 更改为 Button 时,代码崩溃了。
最佳答案
您正在尝试使用 runnable 在 UI 线程中显示 toast ,这就是它出错的原因
Thread background = new Thread(new Runnable() {
public void run() {
// Send message to handler
handler.sendMessage(msgObj);
}
};
private final Handler handler = new Handler() {
public void handleMessage(Message msg) {
//Catch the response and show the toast
String aResponse = msg.getData().getString("message");
Toast.makeText(getBaseContext(),"Not Got Response From Server.",
Toast.LENGTH_SHORT).show();
}
};
关于android - 为什么我可以通过 looper 在非 uithread 中触摸 UI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24752795/
我正在开始一个新的 Android 项目,并且想了解有关 OrmLite 的所有信息 + 使数据库访问远离 UI 线程。 OLD:在我的上一个项目中,我为每个插入/更新/删除调用使用了一个 Threa
虽然在自己的线程上运行长期任务很有用,但为了更新 UI 组件,需要在 UIThread 中运行代码。否则,您的应用程序将在执行期间抛出 CalledFromWrongThreadException。如
doAsync { doSomethingAsync() uiThread { doSomethingUiThread() } } 基于上面的代码。我知道 do
我正在尝试使用 Bluetooth 将 android 数据发送到 arduino .My gui 包含一个 SeekBar和加速度计读数。所有Sockets已成功形成,如果我将数据写入 Output
有人可以向我解释一下 UI 线程到底是什么吗?在 developer.android.com 上,它提到了 runOnUiThread 函数 public final void runOnUiThre
使用 android-annotations 我可以替换 AsyncTask与 @UiThread . 但是可以处理像 AsynkTask 的 onPreExcecute/onPostExecute
当 MediaPlayer 正在播放时,我在 UIThread 上有一个 Runnable 更新 SeekBar。然而,当我切换到另一个 Activity 时,我的应用程序因异常而崩溃,导致 Runn
我正在使用显示用户联系人的 UITableView。对于一些拥有大量联系人(5000 多个联系人)的用户,tableview 也有 5000 多行。 具有约 12k 行的演示案例中的 reloadDa
AndroidAnnotations提供一个注释,允许在 UI 线程上运行一个方法,无论它是从哪个线程调用的,例如 void myMethod() { doInUiThread("hello"
我目前正在使用 Anko 的 doAsync 来处理异步任务。具体来说,我使用它来解析对数据对象的 ArrayList 的响应 json,如下所示: // Asynchronously parse s
我有一个类,它通过 PRISMs 事件聚合器订阅一个事件。 因为模拟事件聚合器有点困难,如 here 所述。 ,我只是实例化一个真实的并将其传递给被测系统。 然后在我的测试中,我通过该聚合器发布事件,
我使用名为“Fuel”的库从服务器获取数据,这是我的代码: button1.setOnClickListener { val jb = "{......}"
标题说明了一切。好吧,主要是。这只发生在 1 台设备上。其他设备没问题。 在 AsyncTask 的 onPostExecute 中,我调用下面的代码,在得到这个异常之前我没有 runOnUiThre
我正在使用 OkHttp3 执行 GET API 调用,该方法有效,但是我想使用 GET 调用的结果更新 UiThread 中的 TextView (jsonTextView)。 我尝试了 10 种不
我是 Android 开发的新手,这是我的第一个应用程序。 问题可能很简单,但我找不到解决方案。 此应用程序是一个简单的 ImageViewer,应用程序必须连接到服务器,并且根据某些数据,服务器将返
第一遍似乎要么使可绘制区域无效,要么绘制背景。第二遍渲染菜单。如果有任何延迟(如下面的示例所示),则会出现灰色方 block 闪烁效果。 这是 Linux 上的 JDK8。 如何停止这种闪烁效果? p
我想在 UiThread 上运行的线程中显示 toast 消息,但在我的调用中似乎未正确引用 Runnable。请在下面查看我非常基本的实现: this.runOnUiThread(new Runna
我以为 @UiThread和 @MainThread是同一件事。 最佳答案 @MainThread 是您启动应用程序时第一个开始运行的线程 @UiThread 从渲染用户界面的主线程开始 同样来自 A
thread = new Thread() { public void run() { super.run(); System.out.
private Handler workerHandler; // instance variable of UIThread 在 onCreate() 方法中 WorkerThread wt = n
我是一名优秀的程序员,十分优秀!