我是Android新手.我想知道Looper课程的作用以及如何使用它.我已阅读Android Looper类文档,但我无法完全理解它.我在很多地方都看过它但却无法理解它的目的.任何人都可以通过定义目的来帮助我,Looper并且如果可能的话也给出一个简单的例子吗?
我有一个应用程序,从网站读取一些数据,然后创建TextViews从网站检索到的内容.我有一个过程通过一个AsyncTask.我已将其设置为如果在尝试从网站读取时出现网络错误,则会显示"重试"按钮.我的代码在第一次运行时运行良好,但是当我尝试从onClick按钮运行代码时,我收到以下错误:
java.lang.RuntimeException: An error occured while executing doInBackground()
(a few lines of error code)
Caused by: java.lang.IllegalStateException: The current thread must have a looper!
Run Code Online (Sandbox Code Playgroud)
我甚至试图将onClick电话称为外部方法,因为我看到有人推荐,但这没有帮助.以下是一些相关代码:
异步任务
private class DownloadListingTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... urls){
showLoadingPage();
try{
return getList(urls[0]);
}
catch (IOException e){
return sharedPreferences.getString("list_cache", "");
}
}
@Override
protected void onPostExecute(String result){
formatList(result);
}
}
Run Code Online (Sandbox Code Playgroud)
通话方式
private void tryDownload(){
DownloadListingTask downloadListingTask = new DownloadListingTask();
downloadListingTask.execute(url);
}
Run Code Online (Sandbox Code Playgroud)
onClick事件
retryButton.setOnClickListener(new …Run Code Online (Sandbox Code Playgroud)