Fragment做的导航滑动

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/donghai1002/article/details/52058224
public*强调内容* class MainActivity extends FragmentActivity {

    // 成员变量
    private ViewPager pager;
    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private TextView tv4;
    private List<Fragment> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 找到控件
        pager = (ViewPager) findViewById(R.id.viewpager);
        tv1 = (TextView) findViewById(R.id.tv1);
        tv2 = (TextView) findViewById(R.id.tv2);
        tv3 = (TextView) findViewById(R.id.tv3);
        tv4 = (TextView) findViewById(R.id.tv4);

        list = new ArrayList<Fragment>();
        Fragment1 fragment1 = new Fragment1();
        Fragment2 fragment2 = new Fragment2();
        Fragment3 fragment3 = new Fragment3();
        Fragment4 fragment4 = new Fragment4();
        list.add(fragment1);
        list.add(fragment2);
        list.add(fragment3);
        list.add(fragment4);

        pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {

            @Override
            public int getCount() {
                return list.size();
            }

            @Override
            public Fragment getItem(int arg0) {
                return list.get(arg0);
            }
        });

        tv1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                pager.setCurrentItem(0);
            }
        });

        tv2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                pager.setCurrentItem(1);
            }
        });

        tv3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                pager.setCurrentItem(2);
            }
        });

        tv4.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                pager.setCurrentItem(3);
            }
        });

    }
public class Fragment1 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment1, container, false);


        return view;
    }
展开阅读全文

Viewpager中的fragment内嵌fragment滑动报错

02-07

02-07 16:26:30.621: E/AndroidRuntime(19951): java.lang.NullPointerExceptionn02-07 16:26:30.621: E/AndroidRuntime(19951): at android.support.v4.app.Fragment.instantiate(Fragment.java:399)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1790)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.support.v4.app.Fragment.performCreate(Fragment.java:1489)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:893)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.support.v4.view.ViewPager.populate(ViewPager.java:1128)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.support.v4.view.ViewPager.populate(ViewPager.java:975)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1505)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.view.View.measure(View.java:15575)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.view.View.measure(View.java:15575)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5107)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.view.View.measure(View.java:15575)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5107)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.view.View.measure(View.java:15575)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5107)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)n02-07 16:26:30.621: E/AndroidRuntime(19951): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2423)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.view.View.measure(View.java:15575)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2033)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1251)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1427)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1144)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4730)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.view.Choreographer.doCallbacks(Choreographer.java:555)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.view.Choreographer.doFrame(Choreographer.java:525)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.os.Handler.handleCallback(Handler.java:615)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.os.Handler.dispatchMessage(Handler.java:92)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.os.Looper.loop(Looper.java:137)n02-07 16:26:30.621: E/AndroidRuntime(19951): at android.app.ActivityThread.main(ActivityThread.java:4947)n02-07 16:26:30.621: E/AndroidRuntime(19951): at java.lang.reflect.Method.invokeNative(Native Method)n02-07 16:26:30.621: E/AndroidRuntime(19951): at java.lang.reflect.Method.invoke(Method.java:511)n02-07 16:26:30.621: E/AndroidRuntime(19951): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)n02-07 16:26:30.621: E/AndroidRuntime(19951): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)n02-07 16:26:30.621: E/AndroidRuntime(19951): at dalvik.system.NativeStart.main(Native Method) 问答

没有更多推荐了,返回首页