FragmentPagerAdapter

求帮忙看下程序错在哪,谢谢


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>



Fragment01

public 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;
    }
}

Fragment02

public 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;
    }
}

Fragment03

public 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;
    }
}


MyAdapter

public 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();
    }
}


MainActivity

public 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);
    }
}


慕粉4089507
浏览 1377回答 3
3回答

蜂之谷

fragment01,fragment02,fragment03 没有初始化呀
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android
Java