为什么我的viewPager崩溃了?

时间:2013-10-15 00:52:21

标签: java android android-viewpager

视图寻呼机在向前移动时工作正常,但是如果我向前或向后移动它,应用程序会崩溃。

这是处理ViewPager适配器

的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;

这里是logcat

  

10-14 21:52:54.600:E / AndroidRuntime(1945):致命异常:主要   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.HalfScreenImageAdapter.instantiateItem(HalfScreenImageAdapter.java:107)   10-14 21:52:54.600:E / AndroidRuntime(1945):在android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)   10-14 21:52:54.600:E / AndroidRuntime(1945):在android.support.v4.view.ViewPager.populate(ViewPager.java:1016)   10-14 21:52:54.600:E / AndroidRuntime(1945):在android.support.v4.view.ViewPager.populate(ViewPager.java:914)   10-14 21:52:54.600:E / AndroidRuntime(1945):在android.support.v4.view.ViewPager $ 3.run(ViewPager.java:244)   10-14 21:52:54.600:E / AndroidRuntime(1945):在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:749)   10-14 21:52:54.600:E / AndroidRuntime(1945):在android.view.Choreographer.doCallbacks(Choreographer.java:562)   10-14 21:52:54.600:E / AndroidRuntime(1945):在android.view.Choreographer.doFrame(Choreographer.java:531)   10-14 21:52:54.600:E / AndroidRuntime(1945):在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:735)   10-14 21:52:54.600:E / AndroidRuntime(1945):在android.os.Handler.handleCallback(Handler.java:730)   10-14 21:52:54.600:E / AndroidRuntime(1945):在android.os.Handler.dispatchMessage(Handler.java:92)   10-14 21:52:54.600:E / AndroidRuntime(1945):在android.os.Looper.loop(Looper.java:137)   10-14 21:52:54.600:E / AndroidRuntime(1945):在android.app.ActivityThread.main(ActivityThread.java:5103)   10-14 21:52:54.600:E / AndroidRuntime(1945):at java.lang.reflect.Method.invokeNative(Native Method)   10-14 21:52:54.600:E / AndroidRuntime(1945):at java.lang.reflect.Method.invoke(Method.java:525)   10-14 21:52:54.600:E / AndroidRuntime(1945):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)   10-14 21:52:54.600:E / AndroidRuntime(1945):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)   10-14 21:52:54.600:E / AndroidRuntime(1945):at dalvik.system.NativeStart.main(Native Method)   10-14 21:52:56.370:I / SQLiteAssetHelper(2005):成功打开数据库product_database   10-14 21:52:56.430:D / dalvikvm(2005):GC_FOR_ALLOC释放59K,9%免费2751K / 3008K,暂停2ms,总计3ms   10-14 21:52:56.430:I / dalvikvm-heap(2005):将堆(frag case)增长到3.950MB,用于1127532字节分配   10-14 21:52:56.460:D / dalvikvm(2005):GC_FOR_ALLOC释放1K,7%免费3851K / 4112K,暂停29ms,总计29ms   10-14 21:52:56.480:W / dalvikvm(2005):threadid = 11:线程退出未捕获的异常(group = 0xb0f36648)   10-14 21:52:56.480:E / AndroidRuntime(2005):致命异常:ModernAsyncTask#1   10-14 21:52:56.480:E / AndroidRuntime(2005):java.lang.RuntimeException:执行doInBackground()时发生错误   10-14 21:52:56.480:E / AndroidRuntime(2005):在android.support.v4.content.ModernAsyncTask $ 3.done(ModernAsyncTask.java:137)   10-14 21:52:56.480:E / AndroidRuntime(2005):at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)   10-14 21:52:56.480:E / AndroidRuntime(2005):at java.util.concurrent.FutureTask.setException(FutureTask.java:219)   10-14 21:52:56.480:E / AndroidRuntime(2005):at java.util.concurrent.FutureTask.run(FutureTask.java:239)   10-14 21:52:56.480:E / AndroidRuntime(2005):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)   10-14 21:52:56.480:E / AndroidRuntime(2005):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:573)   10-14 21:52:56.480:E / AndroidRuntime(2005):at java.lang.Thread.run(Thread.java:841)   10-14 21:52:56.480:E / AndroidRuntime(2005):引起:java.lang.IllegalArgumentException:索引2处的绑定值为null   10-14 21:52:56.480:E / AndroidRuntime(2005):在android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:164)   10-14 21:52:56.480:E / AndroidRuntime(2005):在android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200)   10-14 21:52:56.480:E / AndroidRuntime(2005):在android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)   10-14 21:52:56.480:E / AndroidRuntime(2005):在android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)   10-14 21:52:56.480:E / AndroidRuntime(2005):在android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)   10-14 21:52:56.480:E / AndroidRuntime(2005):在android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)   10-14 21:52:56.480:E / AndroidRuntime(2005):在android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)   10-14 21:52:56.480:E / AndroidRuntime(2005):at com.example.bertin.database.ProductDB.getHotelLocations(ProductDB.java:46)   10-14 21:52:56.480:E / AndroidRuntime(2005):at com.example.bertin.Products.query(Products.java:112)   10-14 21:52:56.480:E / AndroidRuntime(2005):在android.content.ContentProvider.query(ContentProvider.java:744)   10-14 21:52:56.480:E / AndroidRuntime(2005):在android.content.ContentProvider $ Transport.query(ContentProvider.java:199)   10-14 21:52:56.480:E / AndroidRuntime(2005):在android.content.ContentResolver.query(ContentResolver.java:414)   10-14 21:52:56.480:E / AndroidRuntime(2005):在android.content.ContentResolver.query(ContentResolver.java:357)   10-14 21:52:56.480:E / AndroidRuntime(2005):在android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:49)   10-14 21:52:56.480:E / AndroidRuntime(2005):在android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:35)   10-14 21:52:56.480:E / AndroidRuntime(2005):在android.support.v4.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:242)   10-14 21:52:56.480:E / AndroidRuntime(2005):在android.support.v4.content.AsyncTaskLoader $ LoadTask.doInBackground(AsyncTaskLoader.java:51)   10-14 21:52:56.480:E / AndroidRuntime(2005):在android.support.v4.content.AsyncTaskLoader $ LoadTask.doInBackground(AsyncTaskLoader.java:40)   10-14 21:52:56.480:E / AndroidRuntime(2005):在android.support.v4.content.ModernAsyncTask $ 2.call(ModernAsyncTask.java:123)   10-14 21:52:56.480:E / AndroidRuntime(2005):at java.util.concurrent.FutureTask.run(FutureTask.java:234)   10-14 21:52:56.480:E / AndroidRuntime(2005):... 3 more

2 个答案:

答案 0 :(得分:1)

10-14 21:52:54.600: E/AndroidRuntime(1945): FATAL EXCEPTION: main 10-14 21:52:54.600: E/AndroidRuntime(1945): java.lang.IllegalStateException: **The specified child already has a parent. You must call removeView() on the child's parent first.**

您已在inflate

中添加了根视图

View viewLayout = inflater.inflate(R.layout.layout_full_screen_image, container, false);功能。

您无需再次将视图添加到container

答案 1 :(得分:1)

根据您的粉碎日志,如果有父级,则必须先删除视图。停止>

        View parent = viewLayout.getRootView();
        if (parent != null && parent instanceof ViewPager) {
            ((ViewPager) parent).removeView(viewLayout);
            ((ViewPager) container).addView(viewLayout);
        }