选项卡布局内的Recycler视图给出零点异常

时间:2018-03-12 09:01:20

标签: android android-layout android-recyclerview android-inflate

我有这个activity_category有一个tabLayout。在其他选项卡中我有一个recyclerView,它显示来自firebase的项目,如博客文章示例,但我没有图像,它只有文本视图。即使我在OnCreate方法中初始化recyclerView,它也会引发零点异常 这是我的activity_category

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
tools:context="com.solution.engineering.docket_ereceipt.CategoryActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_weight="1"
        app:title="Categories">

    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark"

        <android.support.design.widget.TabItem
            android:id="@+id/tabItemHardware"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hardware" />

        <android.support.design.widget.TabItem
        android:id="@+id/tabItemStationary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stationary" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabItemOthers"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Others" />

    </android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>

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

在其中一个标签项目(即其他标签)中,我有一个看起来像这样的RecyclerView

<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/relativeOthers">

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/othersRecycler"
    app:layoutManager="android.support.v7.widget.LinearLayoutManager">

</android.support.v7.widget.RecyclerView>
</RelativeLayout>

activity_catagory的Java文件使用switch case在标签之间切换。这是一小段代码,它的调用布局来自switch case

public class SectionsPagerAdapter extends FragmentPagerAdapter {
    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                viewOthers tab6 = new viewOthers();
                return tab6;
             default:
                return null;
        }
    }

java文件viewOthers()看起来像这样

public class viewOthers extends Fragment {

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

    return rootView;
}
}

我在CategoryActivity.java

中初始化了recyclerView
public class CategoryActivity extends AppCompatActivity {
public SectionsPagerAdapter mSectionsPagerAdapter;
private RecyclerView mReceipt;
private DatabaseReference mRef;
public ViewPager mViewPager;
public int extrasPosition;

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

    //NOW HERE IT SHOWS NULL POINT EXCEPTION
    mReceipt = (RecyclerView)findViewById(R.id.othersRecycler);
    mReceipt.setHasFixedSize(true);
    mReceipt.setLayoutManager(new LinearLayoutManager(this));

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);
    tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));

    //display the desired fragment
    mViewPager.setCurrentItem(extrasPosition);


}

我不明白为什么?任何帮助表示赞赏。提前致谢。

0 个答案:

没有答案