android fragment中使用viewpager报空指针,vpBanner 这里就是空了
1.MainActivity
public class MainActivity extends FragmentActivity {
private ViewPager vpBanner;
private ViewpagerAdapter vpAdapter;
private List<View> views;
private ImageView[] dots;
private int[] ivids = { R.drawable.banner1, R.drawable.banner2,
R.drawable.banner3, R.drawable.banner4 };
private int[] ids = { R.id.bannerDot1, R.id.bannerDot2, R.id.bannerDot3,
R.id.bannerDot4 };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new MainFragment()).commit();
}
initViews();
initDots();
}
private void initViews() {
views = new ArrayList<View>();
for (int i = 0; i < ivids.length; i++) {
ImageView iv = new ImageView(this);
iv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
iv.setBackgroundResource(ivids[i]);
views.add(iv);
}
// views.add(inflater.inflate(R.layout.banner1, null));
// views.add(inflater.inflate(R.layout.banner2, null));
// views.add(inflater.inflate(R.layout.banner3, null));
// views.add(inflater.inflate(R.layout.banner4, null));
vpAdapter = new ViewpagerAdapter(views, this);
vpBanner = (ViewPager) findViewById(R.id.vpBanner);
vpBanner.setAdapter(vpAdapter);
vpBanner.addOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
for (int i = 0; i < ids.length; i++) {
if (arg0 == i) {
dots[i].setImageResource(R.drawable.dot_select);
} else {
dots[i].setImageResource(R.drawable.dot);
}
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
private void initDots() {
dots = new ImageView[views.size()];
for (int i = 0; i < views.size(); i++) {
dots[i] = (ImageView) findViewById(ids[i]);
}
}
}
2.MainFragment.java
public class MainFragment extends Fragment {
public MainFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
3.ViewpageAdaper.java
public class ViewpagerAdapter extends PagerAdapter{
private List<View> views;
private Context context;
public ViewpagerAdapter(List<View> views,Context context){
this.views = views;
this.context = context;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(views.get(position));
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(views.get(position));
return views.get(position);
}
@Override
public int getCount() {
return views.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return (arg0 == arg1);
}
}
4.activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.demo_viewpagerfm.MainActivity"
tools:ignore="MergeRootFrame" />
5.fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.demo_viewpagerfm.MainActivity$PlaceholderFragment" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="200dp" >
<android.support.v4.view.ViewPager
android:id="@+id/vpBanner"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#888" >
</android.support.v4.view.ViewPager>
<LinearLayout
android:id="@+id/llBanner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:background="#888">
<ImageView
android:id="@+id/bannerDot1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="3dp"
android:src="@drawable/dot_select" />
<ImageView
android:id="@+id/bannerDot2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="3dp"
android:src="@drawable/dot" />
<ImageView
android:id="@+id/bannerDot3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="3dp"
android:src="@drawable/dot" />
<ImageView
android:id="@+id/bannerDot4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="3dp"
android:src="@drawable/dot" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
我不用Fragment 直接在MainActivity中写是可以运行的,用了Fragment后vpBanner赋值这块就找不到了
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
vpBanner 在Fragment里面进行初始化,用rootView去findView
你可以把这个initView和initDot放到fragment中去。
vpBanner = (ViewPager) findViewById(R.id.vpBanner);这个赋值的意思是在当前的activity的布局中寻找id为vpBanner的控件,你的activity_main.xml里没有这个控件。你应该先加载fragment_main.xml这个布局,
fragmentView = getLayoutInflater().inflate(R.layout.more_view,null);然后
vpBanner =(ViewPager) fragmentView.findViewById(R.id.vpBanner);