'Force Close "FATAL EXCEPTION" & "findViewById(com.google.…aterial.R.id.coordinator) must not be null" Kotlin
I try to open new activity from bottomnavbar, but my app force close at first time when i open it, and this is the error message i get.
please someone help me, and thanks for your help.
Error Code
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.apps.test.fx, PID: 24187 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.apps.test.fx/com.apps.test.fx.Dashboard}:java.lang.NullPointerException: findViewById(com.google.…aterial.R.id.coordinator) must not be null
Kotlin Code
private lateinit var chipNavigationBar: ChipNavigationBar
private lateinit var constraint: CoordinatorLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dashboard)
val window = window
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
constraint = findViewById(com.google.android.material.R.id.coordinator)
chipNavigationBar = findViewById(R.id.chip_app_bar)
chipNavigationBar.setItemSelected(R.id.home, true)
supportFragmentManager.beginTransaction().replace(R.id.root_layout, home_fragment()).commit()
constraint.setBackgroundColor(getColor(R.color.dk_purple))
bottom_menu()}
private fun bottom_menu() {
chipNavigationBar.setOnItemSelectedListener({
when(it){
R.id.home ->{
constraint.setBackgroundColor(getColor(R.color.dk_purple))
supportFragmentManager.beginTransaction().replace(R.id.root_layout, home_fragment()).commit()
}
R.id.promo ->{
constraint.setBackgroundColor(getColor(R.color.sft_purple))
supportFragmentManager.beginTransaction().replace(R.id.root_layout, home_fragment()).commit()
}
}
})
}
fun floating_button(view: View){
constraint.setBackgroundColor(getColor(R.color.purple_500))
supportFragmentManager.beginTransaction().replace(R.id.root_layout, home_fragment()).commit()
chipNavigationBar.setItemSelected(R.id.home, true)
val int = Intent(this, floating_action::class.java)
startActivity(int)
}
XMLCode
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:onClick="floating_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:maxImageSize="30dp"
android:layout_marginBottom="40dp"
android:backgroundTint="@color/white"
app:layout_anchor="@id/bottom_app_bar"
android:src="@drawable/ic_category" />
<com.google.android.material.bottomappbar.BottomAppBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bottom_app_bar"
android:layout_gravity="bottom"
android:backgroundTint="@color/dk_green"
style="@style/Widget.MaterialComponents.BottomNavigationView.Colored"
app:fabCradleMargin="6dp"
app:fabAlignmentMode="center"
app:fabCradleRoundedCornerRadius="50dp">
<com.ismaeldivita.chipnavigation.ChipNavigationBar
android:id="@+id/chip_app_bar"
android:layout_gravity="fill_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:padding="10dp"
app:cnb_iconSize="30dp"
android:layout_marginEnd="16dp"
app:cnb_menuResource="@menu/item_menu"
app:cnb_unselectedColor="@color/white" />
</com.google.android.material.bottomappbar.BottomAppBar>
Solution 1:[1]
Here's the error in your stacktrace (each error is caused by the one below, so the last one is the source of the problem):
Caused by: java.lang.NullPointerException: findViewById(com.google.…aterial.R.id.coordinator) must not be null
It's saying that findViewById call is returning null, when it's not allowed to be null. It's not allowed because you're assigning it to constraint, which is defined as a non-null type:
private lateinit var constraint: CoordinatorLayout
If null was allowed it would be ConstraintLayout? instead. findViewById returns null if it can't find a view with the provided ID in the view hierarchy you're doing the lookup on, i.e. the activity's layout you're inflating in setContentView.
So R.layout.activity_dashboard doesn't have a CoordinatorLayout (or any View) with an ID of com.google.android.material.R.id.coordinator. And looking at your XML, that's true! So you need to add one:
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent">
That gives you a resource ID of R.id.coordinator. Any layouts you're creating should have IDs in the R.id. namespace, not android.anything.else.:
constraint = findViewById(R.id.coordinator)
You should only need to access android.R.id... if you're using system-provided layouts like android.R.layout.simple_list_item (or whatever it is) or components like the standard app bar (where the home button has an android.R.id. ID)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | cactustictacs |
