在 Android Studio 中处理具有不同底部导航的多个用户界面

Handeling multiple User Interfaces with different bottom navigations in Android Studio

我正在开发一个应用程序,其中有不同的用户界面,具体取决于您的客户端类型。我想根据登录的用户类型创建不同的底部导航。if- 子句有效,因此日志告诉我正确的用户类型,但我遇到了一个致命异常,因为它告诉我第二个底部的 id导航不存在,但就像第一个一样有效。我现在这不是最干净的方法,但我找不到其他方法。这是我的代码:

这是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) {
            }
        })
}

这是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>

下面是第二个底部导航的xml:

<?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>

更新

这是我遇到的致命异常:

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.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.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) 

就像,这太令人困惑了。我知道它必须与 id 相关,或者在一个布局中不能有两个底部导航这一事实,但我无法以任何其他方式帮助自己,我无法分辨第一个底部导航的第一个 id 在哪里来自,因为它没有在任何地方声明,如果我使用我在 xml 中使用的 id 之一,应用程序崩溃并且我得到相同的致命错误(axception 告诉我引用是空的对象。

好的,所以更新:我找到了正确的 ID。显然有两个现有的 main.xml 文件,我不得不删除一个,但现在我只得到一个底部导航,无论哪个用户(无论是搜索人还是摄影师)登录。问题仍然存在, 我怎样才能把它们分开?
这是更新后的 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>

好的,我通过在新活动中外包底部导航并在主要 activity.kt 的 if 子句中使用新布局启动这些导航来管理它。