使用不带片段的ViewPager时出现IllegalStateException

时间:2016-07-20 12:34:37

标签: android android-viewpager

我正在使用ViewPager在图片之间滑动。我ViewPager不使用任何Fragment。当我滑动到下一个图像时,我的应用程序崩溃并在instantiateItem()中发生异常。这是我的适配器类:

public class SlidePagerAdapter extends PagerAdapter {
LayoutInflater mLayoutInflater;
Context context;
int[] mResources = {
        R.drawable.sports1,
        R.drawable.sports2,
        R.drawable.sports3,
};

public SlidePagerAdapter(Context context) {
    this.context = context;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView = mLayoutInflater.inflate(R.layout.viewpager, container, false);

    ImageView imageView = (ImageView) itemView.findViewById(R.id.viewpager_image);
    imageView.setImageResource(mResources[position]);

       container.addView(imageView);           // HERE MY APPLICATION CRASH
    // container.addView(imageView, position); 
    return imageView;
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == object;
}

@Override
public int getCount() {
    return mResources.length;
}

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

以下是日志详情:

  

java.lang.IllegalStateException:指定的子节点已经有了   parent。您必须先在子级父级上调用removeView()。       在android.view.ViewGroup.addViewInner(ViewGroup.java:3938)       在android.view.ViewGroup.addView(ViewGroup.java:3788)       在android.support.v4.view.ViewPager.addView(ViewPager.java:1413)       在android.view.ViewGroup.addView(ViewGroup.java:3729)       at mycompany.microfinance.SlidePagerAdapter.instantiateItem(SlidePagerAdapter.java:37)       在android.support.v4.view.ViewPager.addNewItem(ViewPager.java:943)       在android.support.v4.view.ViewPager.populate(ViewPager.java:1157)       在android.support.v4.view.ViewPager.populate(ViewPager.java:1025)       在android.support.v4.view.ViewPager $ 3.run(ViewPager.java:254)       在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:767)       在android.view.Choreographer.doCallbacks(Choreographer.java:580)       在android.view.Choreographer.doFrame(Choreographer.java:549)       在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:753)       在android.os.Handler.handleCallback(Handler.java:739)       在android.os.Handler.dispatchMessage(Handler.java:95)       在android.os.Looper.loop(Looper.java:135)       在android.app.ActivityThread.main(ActivityThread.java:5343)       at java.lang.reflect.Method.invoke(Native Method)       在java.lang.reflect.Method.invoke(Method.java:372)       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:905)       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

我没有得到如何解决它。我遵循了一个教程,我认为我正在关注它。请帮助我找出我做错的地方。谢谢。

2 个答案:

答案 0 :(得分:2)

您需要致电container.removeView(((View) object))而不是container.removeView(((View) object).getRootView());

问题是,据我所知,您无法添加已添加的视图,因为您没有删除它,您试图删除其父级

另外,请提供此信息。

@Override
public int getItemPosition(Object object){
    return PagerAdapter.POSITION_NONE;
}

@Override
public boolean isViewFromObject(View view, Object object){
    return view.equals(object);
}

<强>更新

我刚才意识到,你在这一部分的问题是:

View itemView = mLayoutInflater.inflate(R.layout.viewpager, container, false);

ImageView imageView = (ImageView) itemView.findViewById(R.id.viewpager_image);
imageView.setImageResource(mResources[position]);

container.addView(imageView);  

如果imageView是虚增itemView中的某个子视图,则表示您遇到问题,因为您尝试将imageView添加到container,但它已经有父itemView 1}}。所以,或者您需要不是imageView,而是itemView添加到container,或者您需要考虑通货膨胀。

答案 1 :(得分:0)

如果你的其他代码工作正常,这个条件应该有效!

if(imageView.getParent()!=null)
    ((ViewGroup)imageView.getParent()).removeView(imageView); // <- fix
layout.addView(imageView);