Android:java.lang.IllegalStateException:指定的子级已有父级

时间:2014-05-21 05:08:03

标签: android illegalstateexception

这是代码:

package com.example.monoless.adapter;

//Import here

public class TutorialFragment extends Fragment implements OnClickListener {

private ImageView joinWithCoupon;
private ImageView Join;
private Button logIn;
private static Activity mContext;
static View rootView;


public static final String ARG_PAGE = "page";

private int mPageNumber;

// ///////////////////////////
// ScrollView mScrollView;

/**
 * Factory method for this fragment class. Constructs a new fragment for the
 * given page number.
 */
public static TutorialFragment create(Activity context, int pageNumber) {
    TutorialFragment fragment = new TutorialFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_PAGE, pageNumber);
    fragment.setArguments(args);
    mContext = context;
    return fragment;
}

public TutorialFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mPageNumber = getArguments().getInt(ARG_PAGE);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout containing a title and body text.

    ViewHolder viewHolder;
    if (rootView == null || rootView.getTag() == null) {
        viewHolder = new ViewHolder();

        rootView = inflater.inflate(R.layout.fragment_tutorial_page,
                container, false);

        viewHolder.imageview = (ImageView) rootView
                .findViewById(R.id.img_tutorial);
        rootView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) rootView.getTag();
        // ((ViewGroup) rootView.getParent()).removeView(rootView);
    }
    switch (mPageNumber) {
    case 0:
        // imageview.setImageResource(R.drawable.tutorial01);
        viewHolder.imageview
                .setImageBitmap(decodeSampledBitmapFromResource(
                        getResources(), R.drawable.tutorial01, 500, 500));
        break;
    case 1:
        // imageview.setImageResource(R.drawable.tutorial02);
        viewHolder.imageview
                .setImageBitmap(decodeSampledBitmapFromResource(
                        getResources(), R.drawable.tutorial02, 500, 500));
        break;
    case 2:
        // imageview.setImageResource(R.drawable.tutorial03);
        viewHolder.imageview
                .setImageBitmap(decodeSampledBitmapFromResource(
                        getResources(), R.drawable.tutorial03, 500, 500));
        break;
    case 3:
        // imageview.setImageResource(R.drawable.tutorial04);
        viewHolder.imageview
                .setImageBitmap(decodeSampledBitmapFromResource(
                        getResources(), R.drawable.tutorial04, 500, 500));
        break;
    case 4:
        // imageview.setImageResource(R.drawable.tutorial05);
        viewHolder.imageview
                .setImageBitmap(decodeSampledBitmapFromResource(
                        getResources(), R.drawable.tutorial05, 500, 500));
        break;
    case 5:
        // imageview.setImageResource(R.drawable.tutorial06);
        viewHolder.imageview
                .setImageBitmap(decodeSampledBitmapFromResource(
                        getResources(), R.drawable.tutorial06, 500, 500));
        break;
    case 6:
        rootView = (ViewGroup) inflater.inflate(
                R.layout.activity_manual_join, container, false);
        ...
        break;
    default:
        break;
    }

    return rootView;
}

/**
 * Returns the page number represented by this fragment object.
 */
public int getPageNumber() {
    return mPageNumber;
}

@Override
public void onClick(View v) {
    ...
}

public static Bitmap decodeSampledBitmapFromResource(Resources res,
        int resId, int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    // options.inSampleSize = calculateInSampleSize(options, reqWidth,
    // reqHeight);
    options.inSampleSize = 2;
    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

static class ViewHolder {

    ImageView imageview;
}
}

它出现:java.lang.IllegalStateException:指定的子节点已经有父节点。

我真的需要帮助!

0 个答案:

没有答案