【发布时间】:2022-01-15 20:05:04
【问题描述】:
我有一个简单的项目,我想在片段中使用底部表作为列表项,一切看起来都不错,但是在构建项目后,应用程序崩溃了,
row_list.setOnLongClickListener 的空对象引用
我不知道问题出在哪里以及如何解决这个问题,也许我的方法不正确,任何想法将不胜感激。
fragment_list:
<FrameLayout
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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:fitsSystemWindows="true"
tools:context=".view.ListFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/row_list"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="60dp">
</androidx.recyclerview.widget.RecyclerView>
</FrameLayout>
列表片段:
class ListFragment : Fragment() {
override fun onCreate(@Nullable savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
row_list.setOnLongClickListener {
val bottomSheetFragment: BottomSheetDialogFragment = RowItemMenuFragment()
bottomSheetFragment.show(requireFragmentManager(), bottomSheetFragment.tag)
true
}}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_list, container, false)
return view
}}}
RowItemMenuFragment:
class RowItemMenuFragment() : BottomSheetDialogFragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_row_item_menu, container, false)
}
override fun onCreate(@Nullable savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.BottomSheetDialogTheme)
}
}
【问题讨论】:
标签: android kotlin android-bottomsheetdialog