Fragment中的ViewPager和TabLayout。 setObject在null对象引用上

时间:2016-04-22 21:28:56

标签: java android android-fragments android-viewpager

我正在尝试在Fragment中添加viewPager。但是,当我开始申请时,我收到了这个:

  

java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)'

My MainFragment:

public class YourTasksFragment extends Fragment {

@Bind(R.id.viewpager)
ViewPager viewPager;

@Bind(R.id.tabs)
TabLayout tabLayout;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_your_tasks, container, false);
    return v;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    setupViewPager(viewPager);
    tabLayout.setupWithViewPager(viewPager);
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
    adapter.addFragment(new OneFragment(), "ONE");
    adapter.addFragment(new TwoFragment(), "TWO");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> fragmentList = new ArrayList<>();
    private final List<String> fragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager childFragmentManager) {
        super(childFragmentManager);
    }

    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }

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

    public void addFragment(Fragment fragment, String title) {
        fragmentList.add(fragment);
        fragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return fragmentTitleList.get(position);
    }
}
}

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"/>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"  />

2 个答案:

答案 0 :(得分:1)

你忘了在onCreateView上添加Butterknife.bind(this,v)。 这是绑定视图所必需的。

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_your_tasks, container, false);
    Butterknife.bind(this,v);
    return v;
}

答案 1 :(得分:1)

您忘记在onCreateView()中设置Butterknife,因此该变量永远不会被初始化。

这样做:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_your_tasks, container, false);
    Butterknife.bind(this,v);
    return v;
}

希望它有所帮助!