Duc*_*ple 1 android android-asynctask
我有一个应用程序,从网站读取一些数据,然后创建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 View.OnClickListener() {
@Override
public void onClick(View v) {
tryDownload();
}
});
Run Code Online (Sandbox Code Playgroud)
因此,当tryDownload()从onCreateView它调用该方法时,它工作正常,但是当我从那里尝试它onClick时,我得到了那个错误.
小智 5
这段代码对我有用:
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
primaryProgressBar.setVisibility(View.GONE);
}
});Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6249 次 |
| 最近记录: |