在一个片段上设置LayoutManager

我创建了一个片段,我试图实现一个recyclerView并加载一个列表。 当我尝试设置layoutManager时,问题即将到来

错误:

java.lang.NullPointerException:尝试在com.gn.app的空对象引用上调用虚拟方法’void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView $ LayoutManager)’。 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 { override fun onResponse(call: Call?, response: Response?) { if (response?.code() == 200) { loadData(response.body()?.stations) } } override fun onFailure(call: Call?, t: Throwable?) { //TODO: Implement error response. } }) return view } private fun loadData(stations: List?) { 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

    

项目xml

           

例外说rcViewStations是null。 它看起来像使用内部使用getView()的kotlin android扩展。 getView()只能在onCreateView完成后返回一个值。

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

我已经解决了这个问题,在这个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 { override fun onResponse(call: Call?, response: Response?) { if (response?.code() == 200) { loadData(response.body()?.stations) } } override fun onFailure(call: Call?, t: Throwable?) { //TODO: Implement error response. } }) } private fun loadData(stations: List?) { val items = stations?.map { it?.let { Station(name, long, lat) } } rcViewStations.adapter = AdapterStation(items) } }