从FrameLayout中的findviewbyid获取NullPointerException

时间:2017-09-01 16:33:09

标签: android android-fragments nullpointerexception android-recyclerview

调用FindviewbyID()后,我收到nullPointerException。 FragmentLayout2有Recyclerview。

如果我把这个RecyclerView放在MainLayout(这是LinearLayout)我没有得到这个错误,只有当它放在FragmentLayout中时才会出现这个错误。

我在这条线路上收到错误:

  

mRecyclerView =(RecyclerView)findViewById(R.id.rv_list);

这是MainActivity

public class MainActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    RecyclerView.LayoutManager mLayoutManager;
    //ArrayList<String> myDataSet = new ArrayList<>();
    ArrayList<ListClass> myDataSet= new ArrayList<>();
    Fragment fragment;
    android.app.FragmentManager manager;


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

        Fragments2 faFragments2 = new Fragments2();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction()
                .replace(R.id.fragment_bottom, faFragments2 )
                .commit();




        mRecyclerView = (RecyclerView)  findViewById(R.id.rv_list);
        //mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setHasFixedSize(true);

        mLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);

        mRecyclerView.setLayoutManager(mLayoutManager);

        for (int i = 0; i < 30; i++) {
            ListClass listClass= new ListClass();
            listClass.setString1("my String1 " + i);
            listClass.setString2("my String2" + i);
            myDataSet.add(listClass);
        }
        //adapter
        mAdapter = new MyAdapter( myDataSet);
        mRecyclerView.setAdapter(mAdapter);


    }
}

FragmentLayout:

<FrameLayout 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"

    tools:context="layout.Fragments2">

    <!-- TODO: Update blank fragment layout -->
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>


</FrameLayout>

MainActivityLayout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout

        android:id="@+id/fragment_top"
        android:layout_width="match_parent"
        android:layout_height="50dp">

    </FrameLayout>

    <FrameLayout

        android:id="@+id/fragment_bottom"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></FrameLayout>
</LinearLayout>

logcat的:

01 20:32:42.844 30026-30026/com.example.maddy.recyclerviewdemo E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                    Process: com.example.maddy.recyclerviewdemo, PID: 30026
                                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.maddy.recyclerviewdemo/com.example.maddy.recyclerviewdemo.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2449)
                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2509)
                                                                                        at android.app.ActivityThread.access$1000(ActivityThread.java:153)
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                        at android.os.Looper.loop(Looper.java:154)
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:5529)
                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
                                                                                     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
                                                                                        at com.example.maddy.recyclerviewdemo.MainActivity.onCreate(MainActivity.java:43)
                                                                                        at android.app.Activity.performCreate(Activity.java:6303)
                                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2402)
                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2509) 
                                                                                        at android.app.ActivityThread.access$1000(ActivityThread.java:153) 
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373) 
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                        at android.os.Looper.loop(Looper.java:154) 
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:5529) 
                                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739) 
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629) 
09-01 20:32:42.855 30026-30026/com.example.maddy.recyclerviewdemo I/Process: Sending signal. PID: 30026 SIG: 9

2 个答案:

答案 0 :(得分:1)

MainActivity中,您需要将布局设置为mainlayout.xml

    setContentView(R.layout.mainlayout);

但是,您似乎正在尝试访问RecyclerView

中不存在的mainlayout.xml

使用findViewById时,假设RecyclerView位于mainlayout.xml而不是其他内容。

答案 1 :(得分:0)

您不能只从您的Activity访问Fragment视图(这就是它为211的原因)。 如果您的RecyclerView是Fragment布局的一部分,您应该在Fragment类中使用它,而不是Activity。