视图寻呼机在向前移动时工作正常,但如果我将它向后移动然后向前或向后移动,应用程序就会崩溃。
这是处理 ViewPager Adapter 的 java 文件
public class HalfScreenImageAdapter extends PagerAdapter {
MemoryCache memoryCache = new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews = Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;
private Activity _activity;
private String[] _imagePaths;
private LayoutInflater inflater;
// constructor
public HalfScreenImageAdapter(Activity activity,
String [] imagePaths, Context context) {
fileCache = new FileCache(context);
executorService = Executors.newFixedThreadPool(5);
this._activity = activity;
this._imagePaths = imagePaths;
}
int stub_id = R.drawable.ic_launcher;
@Override
public int getCount() {
// TODO Auto-generated method stub
return this._imagePaths.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = _activity;
ImageView imgDisplay = new ImageView(context);
inflater = (LayoutInflater) _activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.layout_full_screen_image, container,
false);
stub_id = R.drawable.hotel_overlay;
imgDisplay = (ImageView) viewLayout.findViewById(R.id.imgDisplay);
imageViews.put(imgDisplay, _imagePaths[position]);
Bitmap bitmap = memoryCache.get(_imagePaths[position]);
if(bitmap != null) {
imgDisplay.setImageBitmap(bitmap);
((ViewPager) container).addView(viewLayout);
//return imgDisplay;
} else {
queuePhoto(_imagePaths[position], imgDisplay);
imgDisplay.setImageResource(R.drawable.hotel_overlay);
}
((ViewPager) container).addView(viewLayout);
return viewLayout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((RelativeLayout) object);
}
private void queuePhoto(String url, ImageView imageView){
PhotoToLoad p = new PhotoToLoad(url, imageView);
executorService.submit(new PhotosLoader(p));
}
private Bitmap getBitmap(String url){
File f = fileCache.getFile(url);
//from SD cache
Bitmap b = decodeFile(f);
if(b != null)
return b;
//from web
try {
Bitmap bitmap = null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
}catch (Exception ex){
ex.printStackTrace();
return null;
}
}
//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
try{
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 100;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
//Task for the queue
private class PhotoToLoad {
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i) {
url = u;
imageView = i;
}
}
class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad){
this.photoToLoad = photoToLoad;
}
@Override
public void run() {
if(imageViewReused(photoToLoad))
return;
Bitmap bmp = getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
if(imageViewReused(photoToLoad))
return;
BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
Activity a = (Activity)photoToLoad.imageView.getContext();
a.runOnUiThread(bd);
}
}
boolean imageViewReused(PhotoToLoad photoToLoad){
String tag = imageViews.get(photoToLoad.imageView);
if(tag==null || !tag.equals(photoToLoad.url))
return true;
return false;
}
//Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable {
Bitmap bitmap;
PhotoToLoad photoToLoad;
public BitmapDisplayer(Bitmap b, PhotoToLoad p){
bitmap = b;
photoToLoad = p;
}
public void run() {
if(imageViewReused(photoToLoad))
return;
if(bitmap != null)
photoToLoad.imageView.setImageBitmap(bitmap);
else
photoToLoad.imageView.setImageResource(stub_id);
}
}
public void clearCache(){
memoryCache.clear();
fileCache.clear();
}
}
该应用程序在此行崩溃。
((ViewPager) container).addView(viewLayout);
return viewLayout;
这是日志猫
10-14 21:52:54.600: E/AndroidRuntime(1945): 致命异常: main 10-14 21:52:54.600: E/AndroidRuntime(1945): java.lang.IllegalStateException: 指定的孩子已经有一个父母。您必须首先在孩子的父母上调用 removeView()。10-14 21:52:54.600: E/AndroidRuntime(1945): 在 android.view.ViewGroup.addViewInner(ViewGroup.java:3509) 10-14 21:52:54.600: E/AndroidRuntime(1945): 在 android. view.ViewGroup.addView(ViewGroup.java:3380) 10-14 21:52:54.600: E/AndroidRuntime(1945): 在 android.support.v4.view.ViewPager.addView(ViewPager.java:1304) 10-14 21:52:54.600: E/AndroidRuntime(1945): 在 android.view.ViewGroup.addView(ViewGroup.java:3325) 10-14 21:52:54.600: E/AndroidRuntime(1945): 在 android.view.ViewGroup .addView(ViewGroup.java:3301) 10-14 21:52:54.600: E/AndroidRuntime(1945): at com.example.bertin。