&引用;动画师只能在活套线程上运行;从资产文件夹(Android)将图像加载到ImageView时出错

&引用;动画师只能在活套线程上运行;从资产文件夹(Android)将图像加载到ImageView时出错,android,imageview,Android,Imageview,我正在尝试构建一些方法,允许我使用图像的名称从本地文件夹一次加载一个.png图像。我已选择从assets文件夹加载,因为res文件夹不按图像名称加载。在阅读了大量问答之后,我提出了以下方法: private void setImage(String imageName) { String imageReference = String.format("%1$s/%2$s", "images", imageName);

我正在尝试构建一些方法,允许我使用图像的名称从本地文件夹一次加载一个.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();
        }
    }
…其中“images”是资产文件夹中的子目录。使用调试器,我可以看到该方法正在成功地拾取图像,但只要它到达代码
imageView.setImageDrawable(drawable)行,模拟器抛出以下错误:
AndroidRuntimeException:动画师只能在活套线程上运行

就我的用法而言,我试图弄明白这意味着什么,但迄今为止都失败了。感谢您提供的任何帮助/建议:)

编辑:这是我最新的stacktrace,自从我发布我的问题以来,它(明显地)发生了轻微的变化:

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$7.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(
//或者在活动中使用
//runOnUiThread(
新的Runnable(){
@凌驾
公开募捐{
setImageDrawable(…);
}
}
);


感谢您花时间回答这个问题。+1感谢您提供了一个不需要在活动中运行代码的答案,但事实并非如此。不幸的是,我在直接复制和粘贴您的代码时出错,但正如建议的那样,我删除了
imageView.setImageDrawable(…)
line with
@Override public void run(){}
,它删除了错误,但调试器显示仍无法访问此行。您能否提供有关如何
setImage()的更多上下文
被调用?错误的原始堆栈跟踪也会有帮助。@davwillev抱歉,我没有意识到我忘记覆盖
run()
@davwillevYeah所以我提供的解决方案修复了给定堆栈跟踪中指出的问题。当您尝试此解决方案时会发生什么?实际上,我认为我的问题现在位于其他地方,需要新的问题解决方案,所以我将接受您的答案-非常感谢!!!