本文介绍了如何在PageViewer中显示3个片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个片段中,我试图创建一个ViewPager,其中包含3个可以在其间滑动的片段.我在了解结构和错误时遇到了麻烦.

Inside a fragment, Im trying to create a ViewPager with 3 fragments that you can swipe between.Im having trouble understanding the structure and the errors Im getting..

public class ContactFragment extends Fragment {

FragmentPagerAdapter adapterViewPager;
private FragmentActivity myContext;

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

    ViewPager vpPager = (ViewPager) rootView.findViewById(R.id.vpPager);
    adapterViewPager = new MyPagerAdapter(myContext.getSupportFragmentManager());
    vpPager.setAdapter(adapterViewPager);

    return rootView;
}

@Override
public void onAttach(Activity activity) {
    myContext=(FragmentActivity) activity;
    super.onAttach(activity);
}

 public static class MyPagerAdapter extends FragmentPagerAdapter {
    private static int NUM_ITEMS = 3;

    public MyPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    // Returns total number of pages
    @Override
    public int getCount() {
        return NUM_ITEMS;
    }

    // Returns the fragment to display for that page
    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0: // Fragment # 0 - This will show FirstFragment
                return FirstFragment.newInstance(0, "Page # 1");
            case 1: // Fragment # 0 - This will show FirstFragment different title
                return FirstFragment.newInstance(1, "Page # 2");
            default:
                return null;
        }
    }

    // Returns the page title for the top indicator
    @Override
    public CharSequence getPageTitle(int position) {
        return "Page " + position;
    }

  }
}

我的ViewPager布局xml看起来像这样:

My ViewPager layout xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

    <android.support.v4.view.ViewPager
    android:id="@+id/vpPager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v4.view.PagerTabStrip
        android:id="@+id/pager_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:paddingBottom="4dp"
        android:paddingTop="4dp" />

    </android.support.v4.view.ViewPager>
</LinearLayout>

这是我的文本片段类,我实例化了以下三个片段:

This is my text Fragment class I instantiate 3 fragments from:

public class FirstFragment extends Fragment {
// Store instance variables
private String title;
private int page;

// newInstance constructor for creating fragment with arguments
public static FirstFragment newInstance(int page, String title) {
    FirstFragment fragmentFirst = new FirstFragment();
    Bundle args = new Bundle();
    args.putInt("someInt", page);
    args.putString("someTitle", title);
    fragmentFirst.setArguments(args);
    return fragmentFirst;
}

// Store instance variables based on arguments passed
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    page = getArguments().getInt("someInt", 0);
    title = getArguments().getString("someTitle");
}

// Inflate the view for the fragment based on layout XML
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_layout, container, false);
    TextView tvLabel = (TextView) view.findViewById(R.id.tvLabel);
    tvLabel.setText(page + " -- " + title);
    return view;
}
}

当我运行该应用并加载该片段时,它看起来是正确的,但是当我尝试在页面查看器中滑动到一个新片段时,它崩溃了:

When i run the app and load the fragment it looks right, but when i try to slide to a new fragment in the pageviewer it crashes:

E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: org.example.magnusluca.drawertestapp, PID: 3624
java.lang.NullPointerException: Attempt to write to field 'android.support.v4.app.FragmentManagerImpl android.support.v4.app.Fragment.mFragmentManager' on a null object reference

推荐答案

使用此

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0: // Fragment # 0 - This will show FirstFragment
            return FirstFragment.newInstance(0, "Page # 1");
        case 1: // Fragment # 0 - This will show FirstFragment different title
            return FirstFragment.newInstance(1, "Page # 2");
        case 2:
            return FirstFragment.newInstance(2, "Page # 3");
        default:
            return new Fragment();
    }
}

您应该有3个切换用例,并且您的默认用例不应该返回 null

You should have 3 switch cases and also your default case should not return null

这篇关于如何在PageViewer中显示3个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 06:59