SwipeRefreshLayout 空指针
SwipeRefresherLayout nullpointer
我想在我的 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>
你在哪里包括 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 文件
在 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
}
从 include
中删除 id
属性 ...
或将 SwipeRefreshLayout
包装到 <layout>
节点中。
因为它的 id
属性可能在合并时被覆盖。
我想在我的 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>
你在哪里包括 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 文件
在 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
}
从 include
中删除 id
属性 ...
或将 SwipeRefreshLayout
包装到 <layout>
节点中。
因为它的 id
属性可能在合并时被覆盖。