Solution 1 :
kotlin-android-extensions
might have gotten in your way. It won’t work since your view is unattached to the fragment’s instance at that time.
Make sure to only create the view in the onCreateView(...)
method, and use it in onViewCreated(...)
. It’s called immediately afterwards so you won’t lose any time and the fragment won’t start uninitialized.
Your code would ideally look like this:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
) = DataBindingUtil.inflate<FragmentLowDesignBinding>(inflater, R.layout.fragment_low_design, container, false)
.also { binding = it }
.root
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
bottomSheetBehavior = BottomSheetBehavior.from<LinearLayout>(binding.standardBottomSheet)
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED)
binding.include.toggleButton.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED)
} else {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED)
}
}
// continue
}
If you’d like to bypass all of that boilerplate, feel free to use teanity 🙂 It makes these things really simple.
Problem :
I created standard bottom sheet xml file
and I included bottom sheet xml inside to fragment CoordinatorLayout.
after compile and run app
I encounterd this error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ourdesign, PID: 8015
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference
at com.google.android.material.bottomsheet.BottomSheetBehavior.from(BottomSheetBehavior.java:1479)
at com.example.ourdesign.design.LowDesignFragment.onCreateView(LowDesignFragment.kt:55)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManagerImpl.java:2100)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1874)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1830)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
i couldn’t find reason
this code is part of fragment kotlin file.
class LowDesignFragment : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
// binding
private lateinit var binding: FragmentLowDesignBinding
// buttonSheet
private lateinit var bottomSheetBehavior : BottomSheetBehavior<LinearLayout>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_low_design, container, false)
bottomSheetBehavior = BottomSheetBehavior.from<LinearLayout>(standardBottomSheet)
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED)
binding.include.toggleButton.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED)
} else {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED)
}
}
return binding.root
}
This code is fragment xml file.
<?xml version="1.0" encoding="utf-8"?>
<layout 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"
tools_context=".MainActivity">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android_layout_width="match_parent"
android_layout_height="match_parent"
android_orientation="vertical">
<!-- TODO: Update blank fragment layout -->
<androidx.constraintlayout.widget.ConstraintLayout
android_id="@+id/constraintLayout2"
android_layout_width="match_parent"
android_layout_height="match_parent">
</androidx.constraintlayout.widget.ConstraintLayout>
<include
android_id="@+id/include"
layout="@layout/low_design_bottom_sheet"
app_layout_anchorGravity="top|center" />
<ImageView
android_id="@+id/imageView"
android_layout_width="403dp"
android_layout_height="659dp"
android_layout_marginStart="16dp"
android_layout_marginTop="16dp"
android_layout_marginEnd="16dp"
android_layout_marginBottom="16dp"
app_layout_anchorGravity="center"
tools_srcCompat="@tools:sample/backgrounds/scenic" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>
This code is bottom sheet xml file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns_android="http://schemas.android.com/apk/res/android"
xmlns_app="http://schemas.android.com/apk/res-auto"
android_id="@+id/standardBottomSheet"
style="@style/bottom_sheet_row_style"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_orientation="vertical"
app_behavior_hideable="true"
app_behavior_peekHeight="?actionBarSize"
app_layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<LinearLayout
android_layout_width="match_parent"
android_layout_height="?actionBarSize"
android_orientation="horizontal">
<ToggleButton
android_id="@+id/toggleButton"
android_layout_width="36dp"
android_layout_height="36dp"
android_background="@drawable/ic_expand_less_black_36dp"
android_textOff=""
android_textOn=""
android_text="" />
<TextView
android_id="@+id/editTool"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_text="Edit Tool"
android_textAlignment="center" />
</LinearLayout>
<LinearLayout
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_orientation="horizontal">
<LinearLayout
style="@style/bottom_sheet_row_style"
android_layout_gravity="center">
<ImageView
style="@style/bottom_sheet_element_image_style"
android_contentDescription="@string/text"
android_src="@drawable/ic_text_format_black_48dp" />
<TextView
style="@style/bottom_sheet_element_text_style"
android_text="@string/text" />
</LinearLayout>
<LinearLayout style="@style/bottom_sheet_row_style">
<ImageView
style="@style/bottom_sheet_element_image_style"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_contentDescription="@string/text"
android_src="@drawable/ic_text_format_black_48dp" />
<TextView
style="@style/bottom_sheet_element_text_style"
android_text="@string/image_size" />
</LinearLayout>
<LinearLayout style="@style/bottom_sheet_row_style">
<ImageView
style="@style/bottom_sheet_element_image_style"
android_src="@drawable/ic_text_format_black_48dp" />
<TextView
style="@style/bottom_sheet_element_text_style"
android_text="@string/template" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Do you know how to fix this problem?
Comments
Comment posted by Hai Hack
add your xml layout here
Comment posted by gigi
I added xml layout files. thank you
Comment posted by Ben Shmuel
Where is
Comment posted by gigi
It still doesn’t work…. Is it possible implement bottom sheet in fragment class file? Every example code is implement standard bottom sheet in activity class file. I can’t find example code.
Comment posted by diareuse
Yes of course. As long as both CoordinatorLayout and given BottomSheetBehavior are in the same xml. BottomSheetBehavior should be direct child of CoordinatorLayout in every case.
Comment posted by gigi
Did you meean direct child of CoordinatorLayout mean use dierctly inside the CoordinatorLayout instead using include tag? and… so. if i want to use bottom sheet. not in main activity. Should i have to create another activity and fragment? Thank you for helping me even though it’s a low-level question..!
Comment posted by diareuse
Yes it has to be the direct child of the given CoordinatorLayout, otherwise it