【问题标题】:Need guidance with bottom_navigation and it需要底部导航和它的指导
【发布时间】:2022-06-23 18:18:12
【问题描述】:

您好,我是学习 kotlin 并尝试制作应用程序以及遵循教程的新手。但无论我看什么视频,bottom_navigation 和它都会出错。

任何人都可以看看我的代码,并可能帮助我弄清楚为什么会出现错误。感谢您的任何意见:)

主要活动.ktl

package com.example.myapplication


import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.fragment.app.Fragment
import com.example.myapplication.Fragments.*
import com.example.myapplication.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(binding.root)

        val homeFragment = HomeFragment()
        val videoFragment = VideoFragment()
        val upcomingFragment = UpcomingFragment()
        val podcastFragment = PodcastFragment()
        val communityFragment = CommunityFragment()

        makeCurrentFragment(homeFragment)

        bottom_navigation.setOnNavigationItemSelectedListener
        when(it.itemId){
                R.id.ic_home_icon_white -> makeCurrentFragment(homeFragment)
                R.id.ic_video_white -> makeCurrentFragment(videoFragment)
                R.id.ic_upcoming_icon -> makeCurrentFragment(upcomingFragment)
                R.id.ic_podcast_white -> makeCurrentFragment(podcastFragment)
                R.id.ic_community_white-> makeCurrentFragment(communityFragment)
            }
        }


    private fun makeCurrentFragment(fragment: Fragment) =
        supportFragmentManager.beginTransaction().apply {
            replace(R.id.fl_wrapper, fragment)
            commit()

  }
}

主 Activity.xml


<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#1E1E1E"
    tools:context=".MainActivity">

    <FrameLayout
        android:id = "@+id/fl_wrapper"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottom_navigation"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView

        android:id = "@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="?android:attr/windowBackground"
        app:itemBackground="@color/design_default_color_primary"
        app:itemIconTint="#fff"
        app:itemTextColor="#fff"
        app:menu="@menu/my_nav" />



</RelativeLayout>```

【问题讨论】:

  • 您面临什么样的错误?你能解释一下吗?

标签: android android-studio kotlin uinavigationbar


【解决方案1】:

您需要分享您面临的错误。但是缺少的第一行代码是初始化绑定到您的活动布局:

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.bottomNavigation.setOnNavigationItemSelectedListener{
            when(it.itemId){
                R.id.ic_home_icon_white -> makeCurrentFragment(homeFragment)
                R.id.ic_video_white -> makeCurrentFragment(videoFragment)
                R.id.ic_upcoming_icon -> makeCurrentFragment(upcomingFragment)
                R.id.ic_podcast_white -> makeCurrentFragment(podcastFragment)
                R.id.ic_community_white-> makeCurrentFragment(communityFragment)
            }
        }
        //Rest of your code...

【讨论】:

  • 我不断收到的错误是未解决的参考:bottom_navigation 和未解决的参考:它
  • 您是否将上述代码添加到您的活动中?之后您还应该使用 binding.bottomNavigation 。我会更新我的答案
猜你喜欢
  • 2022-11-11
  • 1970-01-01
  • 1970-01-01
  • 2015-05-25
  • 1970-01-01
  • 2018-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多