在初始化viewPagerAdapter时,显示异常。从网上找了找有两类这样的问题,一种是说给一个视图设置了两个父类,如:
TextView tv = new TextView();
layout.adView(tv);
layout2.adView(tv);这样就会报异常,需要先在其父视图中释放才能添加到另一个父视图
第二种是说:初始化时使用inflater可能出现异常
View result = inflater.inflate(R.layout.customer_layout, container);
改成下面就可以了,大概意思是不将container作为新创建的view的父视图。
View result = inflater.inflate(R.layout.customer_layout, container, false);
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3562)
at android.view.ViewGroup.addView(ViewGroup.java:3415)
at android.support.v4.view.ViewPager.addView(ViewPager.java:1341)
at android.view.ViewGroup.addView(ViewGroup.java:3360)
at android.view.ViewGroup.addView(ViewGroup.java:3336)
at com.atguigu.beijingnews2.newseconddetailpager.SecondDetailPager$2.instantiateItem(SecondDetailPager.java:69)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:869)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1085)
at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
at android.support.v4.view.ViewPager$3.run(ViewPager.java:250)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:543)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5045)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
最后是我的错误代码:重复将同一个imageView添加到container中也不可以,也属于将一个view对象设置了两个父视图。
解决方案是将ImageView imageView = new ImageView(activity);挪到instantiateItem()中,即创建多个对象就可以了。
ImageView imageView = new ImageView(activity);
@Override
public Object instantiateItem(ViewGroup container, int position) {
x.image().bind(imageView, topimageUris.get(position));
imageViews.add(imageView);
container.addView(imageViews.get(position));
return imageViews.get(position);
}