我正在开发一个应用程序,从服务器检索图像并将其存储在SD卡中。
我将图像的路径存储在本地数据库中,然后在使用viewpager显示图像时使用它们。
当只有两个图像时,一切正常,但当有两个以上的图像时,应用程序会停止运行,并在图像之间滑动时发生。当我滑动两次以上时,应用程序立即停止。

class RequestLoadingMeals extends AsyncTask <Void, Void, Void> {
     protected void onPreExecute(){
          super.onPreExecute();
          PD = new ProgressDialog(Meals.this);
          PD.setMessage("Loading Meals, Please wait...");
          PD.setIndeterminate(false);
          PD.setCancelable(true);
          PD.setProgressStyle(ProgressDialog.STYLE_SPINNER);
          PD.show();
     }

    @Override
    protected Void doInBackground(Void... arg0) {
        // TODO Auto-generated method stub

        myDatabaseHandler.openToRead();
        Cursor meals_cursor = myDatabaseHandler.queueSomeMeals(rest_name);
        if (meals_cursor.moveToFirst())
          {
             do
             {
                 try{
                 images.add(meals_cursor.getString(meals_cursor.getColumnIndex(DatabaseHandler.KEY_MEAL_IMAGE_PATH)));
                 } catch (Exception h){
                 }
             }while (meals_cursor.moveToNext());
          }
        myDatabaseHandler.close();
        return null;
    }
    protected void onPostExecute(final Void x) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(Meals.this, images);
        ViewPager myPager = (ViewPager) findViewById(R.id.mypager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(0);
        PD.dismiss();
    }
}

这里是viewpager类
public class ViewPagerAdapter extends PagerAdapter {

 Activity activity;
 ArrayList<String> imageArray = new ArrayList<String>();


 public ViewPagerAdapter(Activity act, ArrayList<String> images) {
      imageArray = images;
      activity = act;
     }
 public int getCount() {
  return imageArray.size();
 }

 public Object instantiateItem(ViewGroup collection, int position) {
  ImageView view = new ImageView(activity);
  view.setLayoutParams(new LayoutParams());
  view.setScaleType(ScaleType.FIT_XY);
  String path = imageArray.get(position);
  Bitmap myBitmap = BitmapFactory.decodeFile(path);
  view.setImageBitmap(myBitmap);
  ((ViewPager) collection).addView(view, 0);
  return view;
 }
 @Override
 public boolean isViewFromObject(View arg0, Object arg1) {
  return arg0 == ((View) arg1);
 }

 @Override
 public Parcelable saveState() {
  return null;
 }

}

Logcat:
12-02 12:46:38.956: E/AndroidRuntime(431): FATAL EXCEPTION: main
12-02 12:46:38.956: E/AndroidRuntime(431):java.lang.UnsupportedOperationException:Required method destroyItem was not overridden
12-02 12:46:38.956: E/AndroidRuntime(431):  at android.support.v4.view.PagerAdapter.destroyItem(PagerAdapter.java:192)
12-02 12:46:38.956: E/AndroidRuntime(431):  at android.support.v4.view.PagerAdapter.destroyItem(PagerAdapter.java:124)
12-02 12:46:38.956: E/AndroidRuntime(431):  at android.support.v4.view.ViewPager.populate(ViewPager.java:1036)
12-02 12:46:38.956: E/AndroidRuntime(431):  at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
12-02 12:46:38.956: E/AndroidRuntime(431):  at android.support.v4.view.ViewPager$3.run(ViewPager.java:244)
12-02 12:46:38.956: E/AndroidRuntime(431):  at android.os.Handler.handleCallback(Handler.java:587)
12-02 12:46:38.956: E/AndroidRuntime(431):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-02 12:46:38.956: E/AndroidRuntime(431):  at android.os.Looper.loop(Looper.java:123)
12-02 12:46:38.956: E/AndroidRuntime(431):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-02 12:46:38.956: E/AndroidRuntime(431):  at java.lang.reflect.Method.invokeNative(Native Method)
12-02 12:46:38.956: E/AndroidRuntime(431):  at java.lang.reflect.Method.invoke(Method.java:507)
12-02 12:46:38.956: E/AndroidRuntime(431):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-02 12:46:38.956: E/AndroidRuntime(431):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-02 12:46:38.956: E/AndroidRuntime(431):  at dalvik.system.NativeStart.main(Native Method)

最佳答案

将此添加到您的ViewPagerAdapter

@Override
public void destroyItem(View collection, int position, Object view) {
     ((ViewPager) collection).removeView((View) view);
}

我建议不要在instantiateItem中执行繁重的长时间运行任务。在ui线程上解码图像是个坏主意。

07-24 09:36