0

I am new on Android. Yesterday I added RecyclerView and CardView to my project on BlankFragment. It is running without error but when I open it gives "program has been stopped". In tutorial they include RecyclerView into LinearLayout. But in my BlankFragment I have already FragmentLayout, that's why I think I don't need it. Other guys adviced to look at Logcat on androidStudio, after did it I get AndroidRuntime: FATALEXCEPTION: main

Process: com.example.ganz.afex_with_default_navigation, PID: 10058
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ganz.afex_with_default_navigation/com.example.ganz.afex_with_default_navigation.MainActivity}: 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
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
                                                       at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
                                                       at android.os.Handler.dispatchMessage(Handler.java:105)
                                                       at android.os.Looper.loop(Looper.java:164)
                                                       at android.app.ActivityThread.main(ActivityThread.java:6944)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
                                                    Caused by: 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
                                                       at com.example.ganz.afex_with_default_navigation.MainActivity.onCreate(MainActivity.java:46)
                                                       at android.app.Activity.performCreate(Activity.java:7174)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032) 
                                                       at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:105) 
                                                       at android.os.Looper.loop(Looper.java:164) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:6944) 
                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 

On this Logcat I was linked to my MainActivity.java:46. The 46th row stays in the OnCreate method:

lstTovar = new ArrayList<>();
        lstTovar.add(new Tovar_tavsiya("Modadagi kiyimlar", "Tovar kategoriyasi", "Description", R.drawable.tile_bg_1));
        lstTovar.add(new Tovar_tavsiya("Chet eldan uskunalar", "Tovar kategoriyasi", "Description", R.drawable.tile_bg_2));
        lstTovar.add(new Tovar_tavsiya("Tekstil mahsulotlari", "Tovar kategoriyasi", "Description", R.drawable.tile_bg_3));

        RecyclerView myrv = (RecyclerView) findViewById(R.id.recyclerview_id);
        Recyclerview_Adapter myrv_Adapter = new Recyclerview_Adapter(this, lstTovar);
        myrv.setLayoutManager(new GridLayoutManager(this, 3));    ===IT IS THE 46TH ROW===
        myrv.setAdapter(myrv_Adapter);

Here fragment_blank.xml:

<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="com.example.ganz.afex_with_default_navigation.BlankFragment">

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

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

</FrameLayout>

P.S: I have also adapters, activities, java files for them. If I will add them here too I guess the question will be messed. If you need their parts to solve the problem I will share them. Thank you in advance!

Ganz Alex
  • 47
  • 1
  • 8
  • Your `RecyclerView` is `null` – AskNilesh May 25 '18 at 05:56
  • Please check setContentView(...) , It seems you have not called setContentView method – Anand Phadke May 25 '18 at 05:59
  • 2
    If the `RecyclerView` is in the `Fragment`, you should be handling it in the `Fragment`, not in the `Activity`. – Mike M. May 25 '18 at 06:01
  • @NileshRathod Why is null? I did everything which was shown in tutorial. – Ganz Alex May 25 '18 at 06:42
  • @AnandPhadke I didn't use setContentView(...). Why do I need it? – Ganz Alex May 25 '18 at 06:43
  • @GanzAlex did you read this comment https://stackoverflow.com/questions/50522389/recyclerview-cardview-arent-seen-on-blankfragment?noredirect=1#comment88056896_50522389 also can u share that tutorial link – AskNilesh May 25 '18 at 06:43
  • @MikeM. In the same fragment? So, does it mean that I should take and put my all codes from Activity to that Fragment, and also codes from Activity's java file to the Fragment's java file? – Ganz Alex May 25 '18 at 06:47
  • @GanzAlex check this [how to do findveiwid in fragment](https://stackoverflow.com/questions/6495898/findviewbyid-in-fragment) – AskNilesh May 25 '18 at 06:51
  • @NileshRathod Yes, here it is https://www.youtube.com/watch?v=SD2t75T5RdY – Ganz Alex May 25 '18 at 06:53

0 Answers0