获取java.lang.IllegalStateException:当前线程必须有一个looper

Nit*_*ain 5 performance android android-asynctask android-looper

我收到此错误,我的应用程序崩溃:

java.lang.IllegalStateException:当前线程必须有一个looper!

我没有太多关于如何在谷歌上使用looper,我使用线程(主要用于睡眠功能),处理程序(用于在Async任务运行时下载图像)和异步任务(用于从URL获取JSON数据) .我不知道如何解决这个问题,所以任何建议都会有所帮助.

这是单击按钮时执行的线程的代码:

View view = flingContainer.getSelectedView();
          view.findViewById(R.id.item_swipe_right_indicator).setAlpha((float) 1.0);

        Thread timer = new Thread() {
            public void run() {
                try {
                    sleep(320);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    flingContainer.getTopCardListener().selectLeft();
                }
            }
        };
        timer.start();
Run Code Online (Sandbox Code Playgroud)

我正在使用这个 libray和log-cat是: 图片

其中:at com.enormous.quotesgram.MainActivity$3.run(MainActivity.java:479)在log-cat中的最后一行对应flingContainer.getTopCardListener().selectLeft();于上面的代码行:

Tri*_*mon 1

尝试以下操作(不幸的是我无法测试代码):

Thread timer = new Thread() {
    public void run() {
        try {
            sleep(320);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    flingContainer.getTopCardListener().selectLeft();
                }
            });
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

背后的想法是,Timer线程不是线程Looper导致异常“当前线程必须有循环程序”)。然而,UI 线程是一个Looper线程(例如参见此站点)。

由于flingContainer.getTopCardListener().selectLeft()可能设计为在 UI 线程上运行,如果它不在管道线程中调用,它就会失败。