-1

I am getting a nullPointerException after calling FindviewbyID() . a FragmentLayout2 has Recyclerview.

if i put this RecyclerView in MainLayout(which is LinearLayout) i'm not getting this error ,only when it is place in FragmentLayout this error occurs after running.

I'm getting error on this Line:

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

this is 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
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Maddy
  • 19
  • 6
  • If your `RecyclerView` is in the `Fragment`, you should be setting it up in the `Fragment`, not in the `Activity`. – Mike M. Sep 01 '17 at 20:57
  • context of the main activity to which the view has been attached did the work, (contextOfMainActivity).findViewById(); – Maddy Sep 08 '17 at 10:53

2 Answers2

1

In your MainActivity you're setting the layout to mainlayout.xml

    setContentView(R.layout.mainlayout);

However, it seems you're trying to access the RecyclerView which is not in your mainlayout.xml

By using findViewById it is assumed that the RecyclerView is in mainlayout.xml and not something else.

Tristan
  • 3,530
  • 3
  • 30
  • 39
0

You cannot just get access to Fragment views from your Activity (it's the reason why it's null). If your RecyclerView is a part of a Fragment layout you should work with it inside a Fragment class, not Activity.

Vasyl Glodan
  • 556
  • 1
  • 6
  • 22