Cannot find ViewPager’s Id In a PopupWindow

个人主页已迁移到: http://www.blog4app.com

使用ViewPager,运行后Logcat报出如下错误

No view found for id 0x7f080005 (id/view_pager) for fragment AdvertisementImageFragment{419e41d0 #0 id=0x7f080005 android:switcher:2131230725:0}

当时十分纠结为什么找不到view_page的定义,但是明明已经在xml中做好了定义

                   <FrameLayout
		         android:layout_width="match_parent"
		         android:layout_height="244dp"
		         android:layout_gravity="center"
		         android:background="@drawable/car_taxi_vendor_advertisement_bg"
		         android:baselineAligned="false" >
		         <android.support.v4.view.ViewPager
		             android:id="@+id/view_pager"
		             android:layout_width="wrap_content"
		             android:layout_height="wrap_content" />
		         <com.**.view.car.CirclePageIndicator
		             android:id="@+id/indicator"
		             android:layout_width="match_parent"
		             android:layout_height="wrap_content"
		             android:layout_marginBottom="12dp"
		             android:layout_gravity="center_horizontal|bottom" />
		     </FrameLayout>

项目场景: 点击某Activity的Button后,弹出一个居中的小View,在View里有多个可以滑动展示的图片。

最开始解决方案: 使用PopupWindwos,把如上的布局inflator后,赋值给PopupWindows的contentview。ViewPager的Adapter使用的是继承于FragmentAdapter的类。

contentView = inflater.inflate(R.layout.car_taxi_vendor_advertisement_view, null, false);
popupWindow.setContentView(contentView);
mBannerAdapter = new VendorAdvertisementImageAdapter(baseActivity.getSupportFragmentManager, icons, mImageFetcher);
pager.setAdapter(mBannerAdapter);

运行后,始终提示找不到view_pager的定义。

错误原因:因为FragmentAdapter传入的是Activity的FragmentManger,所以默认是在Activity的布局xml中寻找ViewPager的定义,但是实际上它是在弹出的View里定义的。

解决办法:放弃PopupWindows,使用FragmentDialog。注意:在new FragmentAdapter传入getChidFragmentManager。

mBannerAdapter = new VendorAdvertisementImageAdapter(getChildFragmentManager(), icons, mImageFetcher);
		pager.setAdapter(mBannerAdapter);




评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值