我当前的项目涉及从服务器下载数据 - 这大约需要20秒,所以在此期间我想显示进度条,以便用户知道发生了什么。下载任务在DownloadWebpageTask(AsyncTask)中执行,我在preExecute()中设置进度条的可见性,但没有显示出来。我仔细研究了这里的所有问题,但仍然无法实现。如果我没有在onPostExecute()中隐藏进度条,我可以看到发生了什么 - 即使我在onPreExecute()中设置了progBar.setVisibility(0),直到doInBackground之后,它才会真正显示出来。我试着在onPreExecute中设置一个TextView,看看它是否只是进度条不能正常工作,但这也失败了。在预先执行的AsyncTask中没有显示进度条()
请有人可以帮我在这里,我完全卡住!
这就要求我的AsyncTask:
public void getDTCs(View view) throws URISyntaxException{
DtcListSingleton.getInstance().setData(null);
status = "DTC Parsing Not Attempted";
noDTCs = 0;
TextView listTitle = (TextView) findViewById(R.id.DTCtitle);
String newTitle = "Waiting for DTCs";
listTitle.setText(newTitle);
String stringURL = "http://192.168.1.129";
//Check connection
ConnectivityManager cMgr = (ConnectivityManager)getSystemService(this.CONNECTIVITY_SERVICE);
NetworkInfo netInf = cMgr.getActiveNetworkInfo();
// If connected to network carry on with DTC request
if(netInf != null && netInf.isConnected()){
//Download the web page as an InputStream
new DownloadWebpageTask().execute(stringURL);
,这是我的AsyncTask:
private class DownloadWebpageTask extends AsyncTask<String,Void,String>{
@Override
protected void onPreExecute(){
super.onPreExecute();
progBar = (ProgressBar)findViewById(R.id.loadingDTCs);
progBar.setVisibility(0);
TextView title = (TextView)findViewById(R.id.DTCtitle);
title.setText("Loading DTCs");
}
@Override
protected String doInBackground(String...urls){
//params[0] is the url
try{
return downloadUrl(urls[0]);
}
catch(IOException e){
return "Unable to retreieve web page. URL may be invalid.";
}
// DELETE if overcome client.print delay
catch (InterruptedException e) {
e.printStackTrace();
return "Interrupted Exception from Delaying inputstream";
}
}
@Override
protected void onPostExecute(String result){
AlertDialog.Builder resultURL = new AlertDialog.Builder(ActionReadDTCs.this);
resultURL.setTitle("Result of DownloadWebPageTask")
.setMessage(result)
.setPositiveButton("OK", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
progBar = (ProgressBar)findViewById(R.id.loadingDTCs);
progBar.setVisibility(8);
return;
}
})
.show();
}
}
你试过.setVisibility(View.VISIBLE) – PedroHawk 2014-08-29 08:45:20
你的min/max SDK和当前版本的android版本? – 2014-08-29 08:50:41
它改变了文字吗? title.setText(“加载DTC”); ?? – 2014-08-29 08:52:06