查看寻呼机java.lang.IllegalStateException。指定的子项已有父项

时间:2013-07-04 11:59:47

标签: android android-viewpager

我的代码。

package com.pausefablogin;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class ImageAdapter extends PagerAdapter {
Context context;
ArrayList<String> POIImages;
Bitmap iconBitMap;
public ImageAdapter(Context context,ArrayList<String> images){
    POIImages=images;
this.context=context;
System.out.println("Woking here" +images.size());
}
@Override
public int getCount() {
 return POIImages.size();
}

public boolean isViewFromObject(View view, Object object,ArrayList<String> images) {
return view == ((ImageView) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);


imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

((ViewPager) container).addView(imageView, 0);

File photos= new File(POIImages.get(position));
iconBitMap = decodeFile(photos);
iconBitMap = Bitmap.createScaledBitmap(iconBitMap,150, 150, true);

imageView.setImageBitmap(iconBitMap);


((ViewPager) container).addView(imageView, 0); // ERROR thrown here!

return imageView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}



private Bitmap decodeFile(File f){
    try {

        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);


        final int REQUIRED_SIZE=70;
        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++;
        }

        //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;
}
@Override
public boolean isViewFromObject(View view, Object object) {

        return view == ((ImageView) object); 
}
}

粗线显示错误

logcat的

07-04 17:21:17.805: E/AndroidRuntime(931): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=22, result=-1, data=Intent { dat=content://media/external/images/media/19 }} to activity {com.pausefablogin/com.pausefablogin.AddPOI}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
07-04 17:21:17.805: E/AndroidRuntime(931):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2976)
07-04 17:21:17.805: E/AndroidRuntime(931):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3019)
07-04 17:21:17.805: E/AndroidRuntime(931):  at android.app.ActivityThread.access$1100(ActivityThread.java:122)
07-04 17:21:17.805: E/AndroidRuntime(931):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1176)
07-04 17:21:17.805: E/AndroidRuntime(931):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-04 17:21:17.805: E/AndroidRuntime(931):  at android.os.Looper.loop(Looper.java:137)
07-04 17:21:17.805: E/AndroidRuntime(931):  at android.app.ActivityThread.main(ActivityThread.java:4340)
07-04 17:21:17.805: E/AndroidRuntime(931):  at java.lang.reflect.Method.invokeNative(Native Method)
07-04 17:21:17.805: E/AndroidRuntime(931):  at java.lang.reflect.Method.invoke(Method.java:511)
07-04 17:21:17.805: E/AndroidRuntime(931):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-04 17:21:17.805: E/AndroidRuntime(931):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-04 17:21:17.805: E/AndroidRuntime(931):  at dalvik.system.NativeStart.main(Native Method)
07-04 17:21:17.805: E/AndroidRuntime(931): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
07-04 17:21:17.805: E/AndroidRuntime(931):  at android.view.ViewGroup.addViewInner(ViewGroup.java:3344)
07-04 17:21:17.805: E/AndroidRuntime(931):  at android.view.ViewGroup.addView(ViewGroup.java:3215)
07-04 17:21:17.805: E/AndroidRuntime(931):  at android.support.v4.view.ViewPager.addView(ViewPager.java:1300)
07-04 17:21:17.805: E/AndroidRuntime(931):  at android.view.ViewGroup.addView(ViewGroup.java:3172)
07-04 17:21:17.805: E/AndroidRuntime(931):  at com.pausefablogin.ImageAdapter.instantiateItem(ImageAdapter.java:49)
07-04 17:21:17.805: E/AndroidRuntime(931):  at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:829)
07-04 17:21:17.805: E/AndroidRuntime(931):  at android.support.v4.view.ViewPager.populate(ViewPager.java:979)
07-04 17:21:17.805: E/AndroidRuntime(931):  at android.support.v4.view.ViewPager.populate(ViewPager.java:911)
07-04 17:21:17.805: E/AndroidRuntime(931):  at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:440)
07-04 17:21:17.805: E/AndroidRuntime(931):  at com.pausefablogin.AddPOI.onActivityResult(AddPOI.java:361)
07-04 17:21:17.805: E/AndroidRuntime(931):  at android.app.Activity.dispatchActivityResult(Activity.java:4649)
07-04 17:21:17.805: E/AndroidRuntime(931):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2972)
07-04 17:21:17.805: E/AndroidRuntime(931):  ... 11 more

2 个答案:

答案 0 :(得分:3)

您拨打((ViewPager) container).addView(imageView, 0);两次。

答案 1 :(得分:0)

imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

((ViewPager) container).addView(imageView, 0);

File photos= new File(POIImages.get(position));
iconBitMap = decodeFile(photos); iconBitMap = Bitmap.createScaledBitmap(iconBitMap,150, 150, true);

imageView.setImageBitmap(iconBitMap);


((ViewPager) container).addView(imageView, 0);**//Remove this line of code.**