0

我有一个包含2个选项卡的布局,通过片段实现,即每个选项卡都有自己的片段。它们是与片段关联的每个布局中的SwipeRefreshLayout。
还有两个异步任务,它们会对返回JSON数据的脚本进行http调用。
我实现了第一个异步任务,它工作正常,但是当我尝试执行第二个应用程序不断崩溃。登录猫没有多大的帮助,但这些是我看到的最重要的几行:创建一个HTTP请求时
如何运行两个片段,每个都有自己的AsyncTasks

java.lang.RuntimeException: An error occured while executing doInBackground() 

Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 


这些错误发生。任何想法如何解决这个问题?

+0

显示代码AsyncTasks中存在一些问题 –

+0

你在哪里调用第二个AsyncTask? – Carnal

+0

在您的处理程序正上方调用Looper.getMainLooper() = new Handler() –

回答

1

在与UI线程不同的线程中,您必须创建一个Handle并为UI线程发送一个可运行的线程。如下代码

Handler handler = new Handler (Looper.getMainLooper()); 
handler.post (new Runnable() { 
    @Override 
    public void run() { 
     // ... Update view 
    } 
}); 
+0

创建了一个新方法并将处理程序放入其中。从swiperefresh调用,它工作得很好。 – Manny265

相关问题