在我的应用程序中,当启动一个活动时,我会访问一个 URL,并从中获取 XML 文件。XML 文件包含各种图像的 URL。我解析这些值并将它们存储在一个数组列表中。从该列表中,我的网格视图会加载图像。但是,在从 Web 服务加载图像时,有时会出现黑屏的情况。因此,在这种情况下,我想显示进度条。
以下是显示进度条的代码:
但我的应用程序崩溃了。解析函数运行良好,但当它到达了该处时。
以下是显示进度条的代码:
class Image extends AsyncTask<Void, Void, Void>
{
ProgressDialog dialog = new ProgressDialog(ProfileImages.this);
protected void onPreExecute()
{
dialog.setMessage("Please wait...");
dialog.setIndeterminate(true);
dialog.show();
}
protected void onPostExecute(Void unused)
{
i.setImageBitmap(bm);
dialog.dismiss();
}
@Override
protected Void doInBackground(Void... params)
{
parsing();
grid.setAdapter(new ImageAdapter(this));
return null;
}
}
但我的应用程序崩溃了。解析函数运行良好,但当它到达了该处时。
grid.setAdapter(new ImageAdapter(this));
public View getView(final int position, View convertView,ViewGroup parent)
{
ImageView i = new ImageView(this.myContext);
if (convertView == null)
{
i = new ImageView(myContext);
i.setLayoutParams(new GridView.LayoutParams(105, 105));
i.setScaleType(ImageView.ScaleType.CENTER_CROP);
i.setPadding(8, 8, 8, 8);
i.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
dialog = new ProgressDialog(ProfileGridView.this);
dialog.setMessage("Please wait...");
dialog.setIndeterminate(true);
dialog.show();
new Thread()
{
public void run()
{
try
{
Thread.sleep(300);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
Bundle bundle = new Bundle();
bundle.putInt("key1",position);
Appconstant.showLog("position clicked "+position);
Intent myIntent = new Intent(getBaseContext(),ProfileImages.class);
myIntent.putExtras(bundle);
startActivityForResult(myIntent, 0);
dialog.dismiss();
}
}.start();
}
});
}
else
{
i = (ImageView) convertView;
}
try
{
URL aURL = new URL(myRemoteImages[position]);
Appconstant.showLog("Grid View URL " + aURL);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
i.setImageBitmap(bm);
}
catch (IOException e)
{
Appconstant.showErrorLog("DEBUGTAG Remtoe Image Exception" + e);
}
return i;
}
public float getScale(boolean focused, int offset)
{
return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
}
}
它崩溃了。请帮我解决这个问题。