recyclerview不在片段中工作

时间:2017-06-03 21:15:00

标签: android layout android-recyclerview fragment

我有一个标签活动,即时尝试在活动的片段中制作一个recyclerview,当我在运行时在oncreate中声明recyclelerview时会出现以下错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.daneshhamrah.konkuri/com.daneshhamrah.konkuri.Soalate_Konkur}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference

当我在运行时在oncreateview中声明recyclelerview时会出现以下错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTypeface(android.graphics.Typeface)' on a null object reference

此错误适用于recyclerview项目布局

我怎么能解决这个问题?

这是我的片段xml布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.daneshhamrah.konk.Soa_Konk$PlaceholderFragment">

<TextView
    android:id="@+id/section_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:focusableInTouchMode="true">

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="10dp"
                >

                <TextView
                    android:id="@+id/soakonk_frag_txt1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_margin="5dp"
                    android:text="ss" />

                <TextView
                    android:id="@+id/soakonk_frag_txt2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:gravity="center"
                    android:text="yy" />

                <TextView
                    android:text="pp"
                    android:id="@+id/soakonk_frag_txt3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:layout_alignParentRight="true"/>

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

        <android.support.v7.widget.RecyclerView
            android:id="@+id/soakonk_frag_rec"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp">


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


    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

</RelativeLayout>

这是recyclerview项目布局:

<?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="wrap_content"
android:id="@+id/sar_list_single_mainlayout">

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="3dp"
    android:foreground="?attr/selectableItemBackground">

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
        >

        <Button
            android:id="@+id/soakonk_item_soabtn"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:background="@drawable/ic_soa_g"
            android:layout_margin="5dp"
            android:layout_alignParentLeft="true"/>

        <TextView
            android:id="@+id/soakonk_item_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:gravity="center"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#000000" />

        <Button
            android:id="@+id/soakonk_item_pasbtn"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:background="@drawable/ic_pas_g"
            android:layout_margin="5dp"
            android:layout_alignParentRight="true"/>

    </RelativeLayout>

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

</LinearLayout>

这是java片段活动代码:

public class Soa_Konk extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;

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

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

public static class PlaceholderFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }

    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_soa__konk, container, false);
        TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
        TextView tv1 ,tv2 ,tv3 ;
        Typeface vazirTf = Typeface.createFromAsset(getActivity().getAssets(),"font/Vazir.ttf");
        tv1= (TextView)rootView.findViewById(R.id.soakonk_frag_txt1);
        tv2= (TextView)rootView.findViewById(R.id.soakonk_frag_txt2);
        tv3= (TextView)rootView.findViewById(R.id.soakonk_frag_txt3);
        tv1.setTypeface(vazirTf);
        tv2.setTypeface(vazirTf);
        tv3.setTypeface(vazirTf);


        RecyclerView mRecyclerView = (RecyclerView)rootView.findViewById(R.id.soakonk_frag_rec) ;
        RecyclerView.LayoutManager mLayoutManager= new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(mLayoutManager);

        String[] mArrayNames = {"95","94","93","92","91","90","89","88","87","86","85","84","83","82","81","80","79"};
        RecyclerView.Adapter  mAdapter = new MyAdapterSoalateKonkur(mArrayNames,getActivity());
        mRecyclerView.setAdapter(mAdapter);

        return rootView;
    }
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 4;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "Tab A";
            case 1:
                return "Tab B";
            case 2:
                return "Tab C";
            case 3:
                return "Tab D";
        }
        return null;
    }
}
}

1 个答案:

答案 0 :(得分:0)

RecyclerView项发生异常时,请查看您的MyAdapter课程以及您的相关ViewHolder,并检查您是否正确地获得了TextView。< / p>