我设置了ProgressDialog
,但我不会解雇它!它只是继续加载消息,不会结束。我认为我做得对,在onPreExecute
设置了展示消息,将dismiss
设置为onPostExecute
,但有些设置如何无效
继承我的代码:
public class MainActivity extends Activity {
//String[] imgUrls = {getString(R.string.uncc_main_thumb),getString(R.string.football_main_thumb,getString(R.string.ifest_main_thumb),getString(R.string.commencement_main_thumb))};
String[] imgUrls={"http://farm5.staticflickr.com/4113/4843614620_c541de5a5c_m.jpg",
"http://farm8.staticflickr.com/7265/8151547298_85e60e7368_m.jpg",
"http://farm9.staticflickr.com/8054/8414075899_e87a74407b_m.jpg",
"http://farm9.staticflickr.com/8210/8277674769_7d1245dbf1_m.jpg"};
ProgressDialog progressDialog;
ImageView iv1,iv2,iv3,iv4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv1 =(ImageView) findViewById(R.id.imageView1);
iv2 =(ImageView) findViewById(R.id.imageView2);
iv3 =(ImageView) findViewById(R.id.imageView3);
iv4 =(ImageView) findViewById(R.id.imageView4);
for (String imgUrl : imgUrls) {
new DownLoadImages().execute(imgUrl);
}
}
private class DownLoadImages extends
AsyncTask<String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... params) {
String imgUrl = params[0];
Bitmap image = null;
try {
URL url = new URL(imgUrl);
image = BitmapFactory.decodeStream(url.openStream());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return image;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// show the loading message while downloading the image
ProgressDialog.show(MainActivity.this, null, "Loading");
}
@Override
protected void onPostExecute(Bitmap result) {
if (result == null) {
result = ((BitmapDrawable) getResources().getDrawable(
R.drawable.not_found)).getBitmap();
}
//ImageView imageViewToBeAdded = new ImageView(getBaseContext());
// imageViewToBeAdded.setLayoutParams(new LayoutParams(
// LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
// imageViewToBeAdded.setPadding(5, 5, 5, 5);
iv1.setImageBitmap(result);
progressDialog.dismiss();
}
}
}
答案 0 :(得分:2)
您没有创建ProgressDialog的实例。
替换
ProgressDialog.show(MainActivity.this, null, "Loading");
使用
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading");
答案 1 :(得分:0)
使用progressDialog
代替ProgressDialog
来调用ProgressDialog的show
方法:
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setCancelable(true);
progressDialog.setMessage("Loading...");
由于您使用ProgressDialog
调用show方法但尝试使用progressDialog
实例解雇,因此发生了问题。
答案 2 :(得分:0)
在onPreExecute中,你使用className ProgressDialog.show这是一个静态方法,在onPostExecute中你实际上正在使用你的对象来阻止它。
使用你的对象在onPreExecute中启动它,它会起作用。
答案 3 :(得分:0)
实例化对话框并保存参考:
progressDialog = ProgressDialog.show(MainActivity.this, null, "Loading");