[英]How do I properly set SwipeRefreshLayout on Android Studio with kotlin?
I'm trying to use a SwipeRefreshLayout on my project, but there's something wrong... and I don't know what! 我正在尝试在项目中使用SwipeRefreshLayout,但是出了点问题...我不知道该怎么办! Below I show the kotlin
code, XML
and the error
I'm getting. 下面我显示了kotlin
代码, XML
和我遇到的error
。
NOTE, it's a FRAGMENT 注意,这是一个片段
KOTLIN CODE: 科特琳代码:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
swipeRefresh.setOnRefreshListener {
refreshAction() // refresh your list contents somehow
swiperefresh_saloes.isRefreshing = false
}
}
XML CODE XML代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".Activities.Fragments.SaloesFragment">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefresh"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerViewSaloes"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
but I'm getting the following error: 但出现以下错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.SwipeRefreshLayout.setOnRefreshListener(android.support.v4.widget.SwipeRefreshLayout$OnRefreshListener)' on a null object reference at com.zowye.b2b.Activities.Fragments.SaloesFragment.onCreate(SaloesFragment.kt:69) java.lang.NullPointerException:尝试在com.zowye.b2b上的空对象引用上调用虚拟方法'void android.support.v4.widget.SwipeRefreshLayout.setOnRefreshListener(android.support.v4.widget.SwipeRefreshLayout $ OnRefreshListener)'。 Activities.Fragments.SaloesFragment.onCreate(SaloesFragment.kt:69)
I found the error: Since it's a fragment, I can only access the SwipeRefreshLayout after onCreateView inflates the fragment. 我发现了错误:由于这是一个片段,因此我只能在onCreateView扩展该片段之后访问SwipeRefreshLayout。
So, I setted the listener inside onViewCreated
因此,我将侦听器设置为onViewCreated
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
swipeRefresh.setOnRefreshListener {
refreshAction() // refresh your list contents somehow
swipeRefresh.isRefreshing = false // reset the SwipeRefreshLayout (stop the loading spinner)
}
}
OnCreate calls before onCreateView.your view is yet not present when you are calling swipe refresh . 在onCreateView之前的OnCreate调用。调用滑动刷新时,您的视图尚不存在。 Instead put a check before calling this method ie if(activity != null) . 而是在调用此方法之前进行检查,即if(activity!= null)。 And in onCreateView of fragment , provide the view to fragment . 并在片段的onCreateView中提供片段的视图。 Then call this method 然后调用此方法
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_dog_details, container, false)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.