I am developing an app where there are different user interfaces depending on what kind of client u are. I wanted to create different Bottom Navigations depending on what type of user is logged in. The if- clause works, so the Log tells me the correct user type but I am getting a fatal exception because it tells me that the id of the second bottom navigation is not existent, but like the first one works. I now it's not the cleanest way to do so but I couldn't find a different way. Here is my code:
This is the main.kt
class MainActivity : AppCompatActivity() {
lateinit var homeFragment: HomeFragment
lateinit var mapsFragment: MapsFragment
lateinit var profil: Profil
lateinit var chat: Chat
lateinit var homeFoto: HomeFoto
val COARSE_LOCATION_RQ = 101
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val mAuth = FirebaseAuth.getInstance()
lateinit var mDatabase : DatabaseReference
val user = mAuth.currentUser
val uid = user!!.uid
var snapshot: DataSnapshot
var anwender = "Suchender"
mDatabase = FirebaseDatabase.getInstance().getReference("User").child(uid)
//Log.e("keyKey",mDatabase.database.getReference("Anwendertyp").child(anwender).toString())
mDatabase!!.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
if (snapshot.exists() && snapshot.child("Anwendertyp").value.toString() == "Suchender" ){
Log.e("keyKey",snapshot.child("Anwendertyp").value.toString())
//findNavController(R.id.suchenderNavigation)
var bottomnav = findViewById<BottomNavigationView>(R.id.BottomNavMenu)
bottomnav.setOnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_home -> {
homeFragment = HomeFragment()
supportFragmentManager
.beginTransaction()
.replace(R.id.frameLayout, homeFragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit()
}
R.id.mapsFragment -> {
mapsFragment = MapsFragment()
supportFragmentManager
.beginTransaction()
.replace(R.id.frameLayout, mapsFragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit()
}
R.id.navigation_notifications -> {
profil = Profil()
supportFragmentManager
.beginTransaction()
.replace(R.id.frameLayout, profil)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit()
}
}
true
}
}
else{
setContentView(R.layout.startseite_fotografvideograf)
Log.e("key",snapshot.child("Anwendertyp").value.toString())
// Navigation.findNavController(HomeFoto().requireActivity(), R.id.navigation_home_fotografvideograf)
//findNavController(R.id.fotografNavigation)
//This is the variable that triggers the fatal exception
var bottomn = findViewById<BottomNavigationView>(R.id.bottomNavFoto)
Log.e("heyo", bottomn.toString())
bottomn.setOnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_home_fotografvideograf -> {
homeFoto = HomeFoto()
supportFragmentManager
.beginTransaction()
.replace(R.id.frameLayout, homeFoto)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit()
}
R.id.navigation_notifications -> {
profil = Profil()
supportFragmentManager
.beginTransaction()
.replace(R.id.frameLayout, profil)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit()
}
R.id.chatchat -> {
chat = Chat()
supportFragmentManager
.beginTransaction()
.replace(R.id.frameLayout, chat)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit()
}
}
true
}
}
}
override fun onCancelled(error: DatabaseError) {
}
})
}
This is the main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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="#E8E8E8">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="30dp"
android:background="@drawable/buttom_bg"
android:elevation="2dp"
app:itemIconSize="30dp"
app:itemIconTint="@drawable/item_selector"
app:itemRippleColor="@android:color/transparent"
app:labelVisibilityMode="unlabeled"
app:menu="@menu/bottom_nav_menu" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavFoto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="30dp"
android:background="@drawable/buttom_bg"
android:elevation="2dp"
app:itemIconSize="30dp"
app:itemIconTint="@drawable/item_selector"
app:itemRippleColor="@android:color/transparent"
app:labelVisibilityMode="unlabeled"
app:menu="@menu/bottom_nav_fotograf" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
And here is the xml of the second bottom navigation:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/BottomNavFotograf"
>
<item
android:id="@+id/navigation_home_fotografvideograf"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/title_home" />
<item
android:id="@+id/navigation_notifications"
android:icon="@drawable/ic_baseline_person_24"
android:title="@string/profil" />
<item
android:id="@+id/chatchat"
android:icon="@drawable/ic_chat"
android:title="Chat" />
</menu>
Update
This is the FATAL EXCEPTION I'm getting:
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.discoverme, PID: 4430 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.android.material.bottomnavigation.BottomNavigationView.toString()' on a null object reference at com.example.discoverme.MainActivity$onCreate$1.onDataChange(MainActivity.kt:114) at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75) at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63) at – Luzie Ewert 19 hours ago Delete
com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154)
like, it's so confusing. I know it has to be something with the id's or the fact that there can not be two bottom navigations in one layout but I can't help myself any other way and I can't tell where the first id of the first bottom navigation is coming from, because it is nowhere declared and if I'm using one of the id's I used in the xml's the app crashes and I am getting trhe same fatal (axception that tells me the reference is on a null object.
okay so update: I found the correct id. Apparently there have been two existing main.xml files and I had to delete one, but now I am only getting one of the bottom navigations, no matter what user (wether searching person or photographer) is logged in. The question is still, how can I separate them?
Here is the updated xml:
<androidx.constraintlayout.widget.ConstraintLayout
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="#E8E8E8">
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/BottomNavMenu"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_margin="30dp"
android:background="@drawable/buttom_bg"
android:elevation="2dp"
app:itemIconSize="30dp"
app:itemIconTint="@drawable/item_selector"
app:itemRippleColor="@android:color/transparent"
app:labelVisibilityMode="unlabeled"
app:menu="@menu/bottom_nav_menu"
tools:ignore="MissingConstraints">
</com.google.android.material.bottomnavigation.BottomNavigationView>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavFoto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="30dp"
android:background="@drawable/buttom_bg"
android:elevation="2dp"
app:itemIconSize="30dp"
app:itemIconTint="@drawable/item_selector"
app:itemRippleColor="@android:color/transparent"
app:labelVisibilityMode="unlabeled"
app:layout_constraintBottom_toBottomOf="@+id/frameLayout"
app:menu="@menu/bottom_nav_fotograf"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="30dp"></com.google.android.material.bottomnavigation.BottomNavigationView>
</androidx.constraintlayout.widget.ConstraintLayout>
Okay, I managed it by outsourcing the bottom navigations in new activities and starting those in the if-clause of the main activity.kt with new layouts.