【发布时间】:2011-07-22 11:46:05
【问题描述】:
在我的应用程序中,当一项活动启动时,我点击了一个 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)));
}
}
它崩溃了。该怎么做,请帮帮我
【问题讨论】:
-
Uncaughthandler:thread AsyncTask#1 exiting duetouncaughtexception 在 java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) at java.util.concurrent.FutureTask 执行 doInBackground() 时发生错误.setException(FutureTask.java:124) 在 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 在 java.util.concurrent.FutureTask.run(FutureTask.java:137) 在 java.util。 concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561) 在 java.lang.Thread.run(Thread.java:1096)
-
doInBackground() 中的 i 和 bm 是什么?整个方法看起来很奇怪
-
我添加了更多关于 i 和 bm 的代码。 i 指的是图像视图,而 bm 是位图
-
您不能在 doInBackground 中触摸视图,因为此方法在单独的线程中运行。将位图附加到视图应该在 onPostExecute 中完成。
-
grid.setAdapter(new ImageAdapter(this));也应该在 onPostExecute 中进行
标签: android gridview progress-bar