为了账号安全,请及时绑定邮箱和手机立即绑定

FragmentPagerAdapter

FragmentPagerAdapter

慕粉4089507 2017-06-12 12:22:26
求帮忙看下程序错在哪,谢谢activity_main<?xml version="1.0" encoding="utf-8"?> <RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent">         <android.support.v4.view.ViewPager             android:id="@+id/view_pager"             android:layout_width="match_parent"             android:layout_height="match_parent">         </android.support.v4.view.ViewPager> </RelativeLayout>layout01<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"               xmlns:app="http://schemas.android.com/apk/res-auto"               android:orientation="vertical"               android:layout_width="match_parent"               android:layout_height="match_parent">     <ImageView         android:id="@+id/image_view01"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:src="@drawable/a"/> </LinearLayout>layout02<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"               xmlns:app="http://schemas.android.com/apk/res-auto"               android:orientation="vertical"               android:layout_width="match_parent"               android:layout_height="match_parent">     <ImageView         android:id="@+id/image_view02"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:src="@drawable/b"/> </LinearLayout>layout03<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"               xmlns:app="http://schemas.android.com/apk/res-auto"               android:orientation="vertical"               android:layout_width="match_parent"               android:layout_height="match_parent">     <ImageView         android:id="@+id/image_view03"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:src="@drawable/c"/> </LinearLayout>Fragment01public class Fragment01 extends Fragment {     @Nullable     @Override     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {         View view1=inflater.inflate(R.layout.layout01,container,false);         return view1;     } }Fragment02public class Fragment02 extends Fragment {     @Nullable     @Override     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {         View view2=inflater.inflate(R.layout.layout02,container,false);         return view2;     } }Fragment03public class Fragment03 extends Fragment {     @Nullable     @Override     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {         View view3=inflater.inflate(R.layout.layout03,container,false);         return view3;     } }MyAdapterpublic class MyAdapter extends FragmentPagerAdapter {     private List<Fragment> fragmentList;     public myAdapter(FragmentManager fm, List<Fragment> fragmentList) {         super(fm);         this.fragmentList = fragmentList;     }     @Override     public Fragment getItem(int position) {         return fragmentList.get(position);     }     @Override     public int getCount() {         return fragmentList.size();     } }MainActivitypublic class MainActivity extends Activity {     private List<Fragment> fragmentList;     private ViewPager viewPager;     private Fragment fragment01,fragment02,fragment03;     private myAdapter myAdapter;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         fragmentList=new ArrayList<>();         viewPager=(ViewPager)findViewById(R.id.view_pager);         fragmentList.add(fragment01);         fragmentList.add(fragment02);         fragmentList.add(fragment03);         myAdapter=new myAdapter(getSupportFragmentManager(),fragmentList);         viewPager.setAdapter(myAdapter);     } }
查看完整描述

3 回答

已采纳
?
蜂之谷

TA贡献564条经验 获得超863个赞

fragment01,fragment02,fragment03 没有初始化呀

查看完整回答
反对 回复 2017-06-12
  • 3 回答
  • 0 关注
  • 1376 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信