0

I want to have an SwipeRefreshLayout in my ScrollingActivity, so I include the following xml as my content.

But when I start the app I get following exception and I have absolutly no idea whats wrong with it. The ID is right, the SwipeRefresherLayout is there ... maybe because it's in a ScrollingActivity? or Included?

java.lang.RuntimeException: Unable to start activity ComponentInfo{de.corestudio.coinmaster/de.corestudio.coinmaster.PocketMain}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.swiperefreshlayout.widget.SwipeRefreshLayout.setOnRefreshListener(androidx.swiperefreshlayout.widget.SwipeRefreshLayout$OnRefreshListener)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2724)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)
    at android.os.Handler.dispatchMessage(Handler.java:110)
    at android.os.Looper.loop(Looper.java:203)
    at android.app.ActivityThread.main(ActivityThread.java:6247)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.swiperefreshlayout.widget.SwipeRefreshLayout.setOnRefreshListener(androidx.swiperefreshlayout.widget.SwipeRefreshLayout$OnRefreshListener)' on a null object reference
    at de.corestudio.coinmaster.PocketMain.onCreate(PocketMain.java:201)
    at android.app.Activity.performCreate(Activity.java:6666)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2677)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527) 
    at android.os.Handler.dispatchMessage(Handler.java:110) 
    at android.os.Looper.loop(Looper.java:203) 
    at android.app.ActivityThread.main(ActivityThread.java:6247) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924) 

Main.java

SwipeRefreshLayout swipeRefreshLayout;
swipeRefreshLayout = findViewById(R.id.pocketMainRefresh);

swipeRefreshLayout.setOnRefreshListener(new 

SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {

    }
});

include.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout 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/pocketMainRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".PocketMain"
tools:showIn="@layout/activity_pocket_main">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView_placeholder_recyclerview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:text="Füge ein neues Item hinzu"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView_placeholder_recyclervie"
        tools:visibility="visible" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerviewPocketItem"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:visibility="gone">

    </androidx.recyclerview.widget.RecyclerView>

    <ImageView
        android:id="@+id/imageView_placeholder_recyclervie"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/add_icon"
        tools:visibility="visible" />


</androidx.constraintlayout.widget.ConstraintLayout>

J.Doe
  • 177
  • 1
  • 2
  • 13
  • In which activity's lifecycle method did you put this code? – Wang Apr 26 '19 at 14:01
  • @Wang its in the MainActivity, and this activity is a scrollingactivity so with appbar. Maybe its because of that? – J.Doe Apr 26 '19 at 14:04
  • why are you nesting your constraintLayout to SwipeRefresherLayout ? – scienticious Apr 26 '19 at 14:05
  • @scienticious when the recyclerviewadapter is empty some other content will be shown. But even without the constraint and only the recyclerview it's the same exception – J.Doe Apr 26 '19 at 14:06
  • @J.Doe From the logs, seems like the crash is happening at an activity called PocketMain. Do you have 2 activities by any chance? – Rishabh Jain Apr 26 '19 at 14:26
  • @RishabhJain PocketMain.java is the mainactivity – J.Doe Apr 26 '19 at 14:27
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Martin Zeitler Apr 26 '19 at 15:43
  • @MartinZeitler it's just wrong because? If you're answering it would be nice if you are explaining why it is wrong instead of just saying "is just wrong" it's not helpfull – J.Doe Apr 26 '19 at 15:45
  • @J.Doe most likely it cannot find `@+id/pocketMainRefresh` inside the `include`... what I've previously thought that was wrong was just misunderstood due to to poor code-indention...not certain, but moving the `include` inside the `AppBarLayout` might be worth a try (in case it doesn't lay out as it should). – Martin Zeitler Apr 26 '19 at 15:49
  • Well it can find the textview, the imageview ... – J.Doe Apr 26 '19 at 15:52

2 Answers2

0

where are you including SwipeRefreshlayout , you can use simply like this : first of all define your swipeRefreshLayout in xml file .

    <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/pull_to_Refresh"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="@dimen/layout_marginTop_five"
            android:background="@drawable/help_box_bg"
            android:padding="@dimen/padding_three">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="@dimen/layout_marginBottom_ten">


                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recyclearView_outletlist"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

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

            </RelativeLayout>

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

after defining your xml file come to the java file

implements on class header

    SwipeRefreshLayout.OnRefreshListener,
    SwipeRefreshLayout pulltoRefresh;
    pulltoRefresh = view.findViewById(R.id.pull_to_Refresh);
    pulltoRefresh.setOnRefreshListener(this);


   @Override
    public void onRefresh() {

     // do here what do you want
     }
Rocky
  • 457
  • 4
  • 11
0

remove the id attribute from the include ...

or wrap the SwipeRefreshLayout into a <layout> node.

because it's id attribute might be overwritten on merge.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216