Android ImageView setImageResource 在 OS 6.0 中不工作
Android ImageView setImageResource is not working in OS 6.0
imgView.setImageResource(R.drawable.enable);
启用蓝牙的图像在 res 文件夹中,用户 imageView 在 XML 中,它在 nexus 6P(8.1.0) 中运行良好,但应用程序在 OS (6.0.1) 中崩溃)
出现以下错误:
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the
original thread that created a view hierarchy can touch its views.
有没有OS限制?或任何其他方式来解决这个问题?
您正在尝试将其设置在一个线程中。您不能在线程内使用 ui 组件。为此,您必须使用主线程或 ui 线程。您可以尝试在 ui 线程的 运行 中使用它。
runOnUiThread(new Runnable() {
@Override
public void run() {
imgView.setImageResource(R.drawable.enable);
}
});
imgView.setImageResource(R.drawable.enable);
启用蓝牙的图像在 res 文件夹中,用户 imageView 在 XML 中,它在 nexus 6P(8.1.0) 中运行良好,但应用程序在 OS (6.0.1) 中崩溃)
出现以下错误:
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
有没有OS限制?或任何其他方式来解决这个问题?
您正在尝试将其设置在一个线程中。您不能在线程内使用 ui 组件。为此,您必须使用主线程或 ui 线程。您可以尝试在 ui 线程的 运行 中使用它。
runOnUiThread(new Runnable() {
@Override
public void run() {
imgView.setImageResource(R.drawable.enable);
}
});