"Animators may only be run on Looper threads" 将图像从资产文件夹加载到 ImageView 时出错 (Android)
"Animators may only be run on Looper threads" error when loading an image to ImageView from assets folder (Android)
我正在尝试构建一些允许我使用图像名称从本地文件夹一次加载一个 .png 图像的方法。我选择从 assets
文件夹加载,因为 res
文件夹不按图像名称加载。阅读了一些问答后,我得出了以下方法:
private void setImage(String imageName) {
String imageReference = String.format("%1$s/%2$s", "images", imageName);
Context appContext = getContext().getApplicationContext();
AssetManager assetManager = appContext.getAssets();
// get input stream
InputStream inputStream = null;
try {
inputStream = assetManager.open(imageReference);
} catch (IOException e) {
e.printStackTrace();
}
// load image as drawable
Drawable drawable = null;
drawable = Drawable.createFromStream(inputStream, null);
// set image to ImageView
imageView.setImageDrawable(drawable);
try {
inputStream .close();
} catch (IOException e) {
e.printStackTrace();
}
}
...其中“图像”是资产文件夹中的子目录。使用调试器,我可以看到该方法成功地拾取了图像,但是一旦遇到代码行 imageView.setImageDrawable(drawable);
,模拟器就会抛出以下错误:
AndroidRuntimeException: Animators may only be run on Looper threads
根据我的用法,我试图弄清楚这意味着什么,但到目前为止都失败了。任何 help/advice 感激地收到:)
编辑:这是我最新的堆栈跟踪,自从我发布问题后,它(特别地)发生了轻微的变化:
E/AndroidRuntime: FATAL EXCEPTION: Timer-3
Process: com.example.spineapp.debug, PID: 10369
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7753)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1225)
at android.view.View.requestLayout(View.java:23093)
at android.view.View.requestLayout(View.java:23093)
at android.view.View.requestLayout(View.java:23093)
at android.view.View.requestLayout(View.java:23093)
at android.view.View.requestLayout(View.java:23093)
at android.view.View.requestLayout(View.java:23093)
at android.view.View.requestLayout(View.java:23093)
at android.view.View.requestLayout(View.java:23093)
at android.view.View.requestLayout(View.java:23093)
at android.widget.ScrollView.requestLayout(ScrollView.java:1533)
at android.view.View.requestLayout(View.java:23093)
at android.view.View.requestLayout(View.java:23093)
at android.view.View.requestLayout(View.java:23093)
at android.widget.ImageView.setImageDrawable(ImageView.java:571)
at android.support.v7.widget.AppCompatImageView.setImageDrawable(AppCompatImageView.java:100)
at com.spineapp.LeftRightJudgementStepLayout.setImage(LeftRightJudgementStepLayout.java:583)
at com.spineapp.LeftRightJudgementStepLayout.startQuestion(LeftRightJudgementStepLayout.java:521)
at com.spineapp.LeftRightJudgementStepLayout.startNextQuestionOrFinish(LeftRightJudgementStepLayout.java:1031)
at com.spineapp.LeftRightJudgementStepLayout.run(LeftRightJudgementStepLayout.java:482)
at java.util.TimerThread.mainLoop(Timer.java:562)
at java.util.TimerThread.run(Timer.java:512)
您可能正在从非 UI 线程调用 setImageDrawable()
。
尝试在 UI 线程上安排 setImageDrawable
。
imageView.post(
// or inside an activity use
// runOnUiThread(
new Runnable() {
@Override
public void run() {
imageView.setImageDrawable(...);
}
}
);
https://developer.android.com/reference/android/view/View#post(java.lang.Runnable)
https://developer.android.com/reference/android/app/Activity#runOnUiThread(java.lang.Runnable)
我正在尝试构建一些允许我使用图像名称从本地文件夹一次加载一个 .png 图像的方法。我选择从 assets
文件夹加载,因为 res
文件夹不按图像名称加载。阅读了一些问答后,我得出了以下方法:
private void setImage(String imageName) {
String imageReference = String.format("%1$s/%2$s", "images", imageName);
Context appContext = getContext().getApplicationContext();
AssetManager assetManager = appContext.getAssets();
// get input stream
InputStream inputStream = null;
try {
inputStream = assetManager.open(imageReference);
} catch (IOException e) {
e.printStackTrace();
}
// load image as drawable
Drawable drawable = null;
drawable = Drawable.createFromStream(inputStream, null);
// set image to ImageView
imageView.setImageDrawable(drawable);
try {
inputStream .close();
} catch (IOException e) {
e.printStackTrace();
}
}
...其中“图像”是资产文件夹中的子目录。使用调试器,我可以看到该方法成功地拾取了图像,但是一旦遇到代码行 imageView.setImageDrawable(drawable);
,模拟器就会抛出以下错误:
AndroidRuntimeException: Animators may only be run on Looper threads
根据我的用法,我试图弄清楚这意味着什么,但到目前为止都失败了。任何 help/advice 感激地收到:)
编辑:这是我最新的堆栈跟踪,自从我发布问题后,它(特别地)发生了轻微的变化:
E/AndroidRuntime: FATAL EXCEPTION: Timer-3
Process: com.example.spineapp.debug, PID: 10369
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7753)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1225)
at android.view.View.requestLayout(View.java:23093)
at android.view.View.requestLayout(View.java:23093)
at android.view.View.requestLayout(View.java:23093)
at android.view.View.requestLayout(View.java:23093)
at android.view.View.requestLayout(View.java:23093)
at android.view.View.requestLayout(View.java:23093)
at android.view.View.requestLayout(View.java:23093)
at android.view.View.requestLayout(View.java:23093)
at android.view.View.requestLayout(View.java:23093)
at android.widget.ScrollView.requestLayout(ScrollView.java:1533)
at android.view.View.requestLayout(View.java:23093)
at android.view.View.requestLayout(View.java:23093)
at android.view.View.requestLayout(View.java:23093)
at android.widget.ImageView.setImageDrawable(ImageView.java:571)
at android.support.v7.widget.AppCompatImageView.setImageDrawable(AppCompatImageView.java:100)
at com.spineapp.LeftRightJudgementStepLayout.setImage(LeftRightJudgementStepLayout.java:583)
at com.spineapp.LeftRightJudgementStepLayout.startQuestion(LeftRightJudgementStepLayout.java:521)
at com.spineapp.LeftRightJudgementStepLayout.startNextQuestionOrFinish(LeftRightJudgementStepLayout.java:1031)
at com.spineapp.LeftRightJudgementStepLayout.run(LeftRightJudgementStepLayout.java:482)
at java.util.TimerThread.mainLoop(Timer.java:562)
at java.util.TimerThread.run(Timer.java:512)
您可能正在从非 UI 线程调用 setImageDrawable()
。
尝试在 UI 线程上安排 setImageDrawable
。
imageView.post(
// or inside an activity use
// runOnUiThread(
new Runnable() {
@Override
public void run() {
imageView.setImageDrawable(...);
}
}
);
https://developer.android.com/reference/android/view/View#post(java.lang.Runnable)
https://developer.android.com/reference/android/app/Activity#runOnUiThread(java.lang.Runnable)