是否可以在不扩展异步任务的类中使用或显示进度条?我正在尝试创建一个图片库,当它加载所有图片时,我想在用户等待图片库出现时显示进度条。
下面的代码是我试图在后台加载的代码:
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
Cursor imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,null, orderBy + " DESC");
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
this.count = imagecursor.getCount();
this.thumbnails = new Bitmap[this.count];
this.arrPath = new String[this.count];
this.thumbnailsselection = new boolean[this.count];
for (int i = 0; i < this.count; i++) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(getApplicationContext().getContentResolver(), id,
MediaStore.Images.Thumbnails.MICRO_KIND, null);
arrPath[i]= imagecursor.getString(dataColumnIndex);
}
final GridView imagegrid = (GridView) findViewById(R.id.grid_GalleryImage);
imageAdapter = new ImageAdapter(this, false);
imagegrid.setAdapter(imageAdapter);
错误得到:
09-30 10:08:00.560: W/System.err(9283): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
09-30 10:08:00.565: W/System.err(9283): at android.view.ViewRoot.checkThread(ViewRoot.java:3518)
09-30 10:08:00.565: W/System.err(9283): at android.view.ViewRoot.invalidateChild(ViewRoot.java:604)
09-30 10:08:00.565: W/System.err(9283): at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:652)
09-30 10:08:00.565: W/System.err(9283): at android.view.ViewGroup.invalidateChild(ViewGroup.java:3693)
09-30 10:08:00.565: W/System.err(9283): at android.view.View.invalidate(View.java:7228)
09-30 10:08:00.565: W/System.err(9283): at android.view.View.invalidate(View.java:7183)
09-30 10:08:00.565: W/System.err(9283): at android.widget.AbsListView.resetList(AbsListView.java:1749)
09-30 10:08:00.565: W/System.err(9283): at android.widget.GridView.setAdapter(GridView.java:199)
09-30 10:08:00.565: W/System.err(9283): at com.fdf.ireport.S_6th_ImageGallery.doWork(S_6th_ImageGallery.java:245)
09-30 10:08:00.565: W/System.err(9283): at com.fdf.ireport.S_6th_ImageGallery$1.run(S_6th_ImageGallery.java:96)
09-30 10:08:00.565: W/System.err(9283): at java.lang.Thread.run(Thread.java:1020)
09-30 10:08:00.565: W/dalvikvm(9283): threadid=9: thread exiting with uncaught exception (group=0x4026d760)