我创建了一个Fragment,我试图实现一个recyclerView并加载一个列表。当我尝试设置 layoutManager
时,问题就出现了错误:
java.lang.NullPointerException:尝试调用虚方法' void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView $布局管理)' 在null对象引用上 在 com.gn.app.Fragments.StationsFragment.onCreateView(StationsFragment.kt:37)
我不能犯下什么错误。
片段:
class StationsFragment : Fragment() {
companion object {
val TAG: String = StationsFragment::class.java.simpleName
fun newInstance() = StationsFragment()
}
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
activity.title = getString(R.string.title_home)
val view = inflater?.inflate(R.layout.fragment_stations, container, false)
rcViewStations.layoutManager = LinearLayoutManager(activity)
val retrofit = Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build()
val stations = retrofit.create(StationService::class.java)
val call = stations.getStations()
call.enqueue(object : Callback<ResponseStations> {
override fun onResponse(call: Call<ResponseStations>?, response: Response<ResponseStations>?) {
if (response?.code() == 200) {
loadData(response.body()?.stations)
}
}
override fun onFailure(call: Call<ResponseStations>?, t: Throwable?) {
//TODO: Implement error response.
}
})
return view
}
private fun loadData(stations: List<StationsItem?>?) {
val items = stations?.map {
it?.let {
Station(name, long, lat)
}
}
rcViewStations.adapter = AdapterStation(items)
}
}
的Utils
fun ViewGroup.inflate(layoutId: Int): View {
return LayoutInflater.from(context).inflate(layoutId, this, false)
}
片段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"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.gn.app.Fragments.StationsFragment">
<android.support.v7.widget.RecyclerView
android:id="@+id/rcViewStations"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:behavior_overlapTop="30dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</FrameLayout>
item xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/txtStationName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:transitionName="transitionTitle"
tools:text="Name" />
<TextView
android:id="@+id/txtStationDistance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:transitionName="transitionDesc"
tools:text="0.00 m" />
<TextView
android:id="@+id/txtStationBikes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:transitionName="transitionDesc"
tools:text="12" />
<TextView
android:id="@+id/txtStationSlots"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:transitionName="transitionDesc"
tools:text="22" />
<TextView
android:id="@+id/txtStationDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@color/colorAccent"
android:transitionName="transitionPrice"
tools:text="Date" />
</LinearLayout>
</android.support.v7.widget.CardView>
答案 0 :(得分:0)
rcViewStations
为空,例外情况如何。
看起来你正在使用内部使用getView()
的kotlin android扩展。
getView()只能在onCreateView完成后返回一个值。
您可以在onViewCreated
中执行所有视图操作逻辑,也可以在findViewById
inflater?.inflate(R.layout.fragment_stations, container, false)
访问RecyclerView
答案 1 :(得分:0)
我已修复了设置此onActivityCreated
内部布局的问题,因为未创建视图。
所以这个课程看起来像这样:
class StationsFragment : Fragment() {
companion object {
val TAG: String = StationsFragment::class.java.simpleName
fun newInstance() = StationsFragment()
}
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
activity.title = getString(R.string.title_home)
val view = inflater?.inflate(R.layout.fragment_stations, container, false)
return view
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
rcViewStations.layoutManager = LinearLayoutManager(activity)
val retrofit = Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build()
val stations = retrofit.create(StationService::class.java)
val call = stations.getStations()
call.enqueue(object : Callback<ResponseStations> {
override fun onResponse(call: Call<ResponseStations>?, response: Response<ResponseStations>?) {
if (response?.code() == 200) {
loadData(response.body()?.stations)
}
}
override fun onFailure(call: Call<ResponseStations>?, t: Throwable?) {
//TODO: Implement error response.
}
})
}
private fun loadData(stations: List<StationsItem?>?) {
val items = stations?.map {
it?.let {
Station(name, long, lat)
}
}
rcViewStations.adapter = AdapterStation(items)
}
}