When i try to launch the app on my Pixel 4 API 31 it keeps stopping. It starts and throws me out of the app, not even going to the start. I don’t recognize visible errors. Here is my code:
Main activity:
import android.os.Bundle
import android.widget.TextView
import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.example.ecomap00.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var textView: TextView
private val onNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.nav_home -> {
textView.setText("Home")
return@OnNavigationItemSelectedListener true
}
R.id.nav_search -> {
textView.setText("Search")
return@OnNavigationItemSelectedListener true
}
R.id.nav_add_post -> {
textView.setText("Add post")
return@OnNavigationItemSelectedListener true
}
R.id.nav_notifications -> {
textView.setText("Notifications")
return@OnNavigationItemSelectedListener true
}
R.id.nav_profile -> {
textView.setText("Profile")
return@OnNavigationItemSelectedListener true
}
}
false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val navView: BottomNavigationView = findViewById(R.id.nav_view)
textView = findViewById(R.id.message)
navView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener)
}
}
My activity_main.xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="Hello"
android:layout_centerVertical="true"
android:textAlignment="center"
android:textSize="30dp"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:layout_alignParentBottom="true"
app:menu="@menu/bottom_nav_menu"
app:labelVisibilityMode="unlabeled"
/>
</RelativeLayout>
My bottom_nav_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_home"
android:icon="@drawable/home"
android:title="" />
<item
android:id="@+id/nav_search"
android:icon="@drawable/search"
android:title="" />
<item
android:id="@+id/nav_add_post"
android:icon="@drawable/add"
android:title="" />
<item
android:id="@+id/nav_notifications"
android:icon="@drawable/heart"
android:title="" />
<item
android:id="@+id/nav_profile"
android:icon="@drawable/profile_icon"
android:title="" />
</menu>
I also have a couple of still empty xml files that I will use later, I don’t know if that could be the reason. If someone could tell what the problem is I would be glad to know.
2
Answers
It is crashing because, you are not setting any
view
to youractivity
and trying to find thetext
andbottomNav
, addsetContentView
inoncreate
aftersuper
callYou are missing setContentView() method for your activity, so it can not find your view like TextView…
https://developer.android.com/reference/android/app/Activity#setContentView(int)