SwipeRefresherLayout空指针

时间:2019-04-26 13:57:28

标签: android android-layout swiperefreshlayout

我想在ScrollingActivity中使用SwipeRefreshLayout,因此我将以下xml作为内容。

但是,当我启动该应用程序时,出现以下异常,我绝对不知道它有什么问题。 ID是正确的,那里有SwipeRefresherLayout ...也许是因为它在ScrollingActivity中?还是包含?

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>

2 个答案:

答案 0 :(得分:0)

您包括 SwipeRefreshlayout 在内,可以这样简单地使用: 首先在xml文件中定义您的swipeRefreshLayout。

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

定义您的 xml 文件后,将其放入Java文件

类标题上的实现

    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
     }

答案 1 :(得分:0)

id中删除include属性...

或将SwipeRefreshLayout包裹到<layout>节点中。

因为它的id属性在合并时可能会被覆盖。