gpt4 book ai didi

android - 在 fragment 上设置LayoutManager

转载 作者:行者123 更新时间:2023-12-02 13:00:25 24 4
gpt4 key购买 nike

我创建了一个Fragment,在其中尝试实现recyclerView并加载列表。当我尝试设置 layoutManager 时,问题来了

错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference at 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)
}
}

实用程序
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>

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

最佳答案

rcViewStations为null,异常如何表示。
看来您正在使用内部使用getView()的kotlin android扩展。
getView()只能在onCreateView完成后返回一个值。

您可以使用onViewCreated进行所有 View 操作逻辑,也可以通过findViewById在新创建的 View 上使用inflater?.inflate(R.layout.fragment_stations, container, false)来访问RecyclerView

关于android - 在 fragment 上设置LayoutManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47400681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com