在Android Kotlin中实现底部导航栏时出错
我是android开发的新手。 我尝试在我的应用程序中实现导航栏,从片段A切换到片段B。 在片段A中有一个浮动操作按钮,它指向片段c。 我通过导航栏从片段A切换到片段B并没有问题,但当我尝试从浮动操作按钮切换到片段c时,它给了我错误 java.lang.IllegalStateException:View androidx.constraintlayout.widget.constraintlayout{65bf79f V.E……...0,0-10801529#7f0800b2 app:id/frameLayout}没有导航控制器集在Android Kotlin中实现底部导航栏时出错,android,android-fragments,android-navigation-bar,Android,Android Fragments,Android Navigation Bar,我是android开发的新手。 我尝试在我的应用程序中实现导航栏,从片段A切换到片段B。 在片段A中有一个浮动操作按钮,它指向片段c。 我通过导航栏从片段A切换到片段B并没有问题,但当我尝试从浮动操作按钮切换到片段c时,它给了我错误 java.lang.IllegalStateException:View androidx.constraintlayout.widget.constraintlayout{65bf79f V.E……...0,0-10801529#7f0800b2 app:id/f
class MainActivity : AppCompatActivity() {
private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val dateView = findViewById<TextView>(R.id.dateTextView)
dateView.text = getMonth()
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_frag) as NavHostFragment
navController = navHostFragment.navController
setupActionBarWithNavController(navController)
val chartFrag = ChartsFragment()
val infoData = info_data()
val botNav = findViewById<BottomNavigationView>(R.id.botNavigation)
botNav.setOnNavigationItemSelectedListener {
when(it.itemId){
R.id.menu_statement ->{
setCurrentFragment(infoData)
}
R.id.menu_charts ->{
setCurrentFragment(chartFrag)
}
}
true
}
}
fun setCurrentFragment(fragment: Fragment) = supportFragmentManager.beginTransaction().apply{
replace(R.id.nav_host_frag, fragment)
commit()
}
fun getMonth():String{
val calendar = Calendar.getInstance()
val simpleDateFormat = SimpleDateFormat("MMM yyyy")
val date = simpleDateFormat.format(calendar.time)
return date
}
override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp() || super.onSupportNavigateUp()
}
}
class info_data : Fragment() {
val dataSet = dataSource().loadData()
val adapter = RecyclerViewAdapter(dataSet)
companion object{
var income = 0.0
var expense = 0.0
}
lateinit var navController: NavController
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_info_data, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val tvExpense = view.findViewById<TextView>(R.id.tvExpense)
tvExpense.text = "Expense : \n$ "+expense.toString()
val tvIncome = view.findViewById<TextView>(R.id.tvIncome)
tvIncome.text = "Income : \n$ "+income.toString()
val tvTotal = view.findViewById<TextView>(R.id.tvTotal)
tvTotal.text = "Total : \n$ "+grandTotal().toString()
val rv= view.findViewById<RecyclerView>(R.id.recyclerView)
rv.adapter = adapter
rv.layoutManager = LinearLayoutManager(activity)
val floatingBtn = view.findViewById<FloatingActionButton>(R.id.fabBtn)
floatingBtn.setOnClickListener(){
goToNextScreen(view)
}
}
fun goToNextScreen(view: View){
navController = Navigation.findNavController(view)
navController!!.navigate(R.id.action_info_data_to_form_data2)
}
fun grandTotal():Double{
val inc = income
val exp = expense
val total = inc-exp
return total
}
}
代码如下:
MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val dateView = findViewById<TextView>(R.id.dateTextView)
dateView.text = getMonth()
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_frag) as NavHostFragment
navController = navHostFragment.navController
setupActionBarWithNavController(navController)
val chartFrag = ChartsFragment()
val infoData = info_data()
val botNav = findViewById<BottomNavigationView>(R.id.botNavigation)
botNav.setOnNavigationItemSelectedListener {
when(it.itemId){
R.id.menu_statement ->{
setCurrentFragment(infoData)
}
R.id.menu_charts ->{
setCurrentFragment(chartFrag)
}
}
true
}
}
fun setCurrentFragment(fragment: Fragment) = supportFragmentManager.beginTransaction().apply{
replace(R.id.nav_host_frag, fragment)
commit()
}
fun getMonth():String{
val calendar = Calendar.getInstance()
val simpleDateFormat = SimpleDateFormat("MMM yyyy")
val date = simpleDateFormat.format(calendar.time)
return date
}
override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp() || super.onSupportNavigateUp()
}
}
class info_data : Fragment() {
val dataSet = dataSource().loadData()
val adapter = RecyclerViewAdapter(dataSet)
companion object{
var income = 0.0
var expense = 0.0
}
lateinit var navController: NavController
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_info_data, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val tvExpense = view.findViewById<TextView>(R.id.tvExpense)
tvExpense.text = "Expense : \n$ "+expense.toString()
val tvIncome = view.findViewById<TextView>(R.id.tvIncome)
tvIncome.text = "Income : \n$ "+income.toString()
val tvTotal = view.findViewById<TextView>(R.id.tvTotal)
tvTotal.text = "Total : \n$ "+grandTotal().toString()
val rv= view.findViewById<RecyclerView>(R.id.recyclerView)
rv.adapter = adapter
rv.layoutManager = LinearLayoutManager(activity)
val floatingBtn = view.findViewById<FloatingActionButton>(R.id.fabBtn)
floatingBtn.setOnClickListener(){
goToNextScreen(view)
}
}
fun goToNextScreen(view: View){
navController = Navigation.findNavController(view)
navController!!.navigate(R.id.action_info_data_to_form_data2)
}
fun grandTotal():Double{
val inc = income
val exp = expense
val total = inc-exp
return total
}
}
class MainActivity:AppCompatActivity(){
专用lateinit var navController:navController
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val dateView=findviewbyd(R.id.dateTextView)
dateView.text=getMonth()
val navHostFragment=supportFragmentManager.findFragmentById(R.id.nav_host_frag)作为navHostFragment
navController=navHostFragment.navController
setupActionBarWithNavController(navController)
val chartFrag=ChartsFragment()
val infoData=info_data()
val botNav=findviewbyd(R.id.botNavigation)
botNav.setOnNavigationItemSelectedListener{
何时(it.itemId){
R.id.菜单\声明->{
setCurrentFragment(信息数据)
}
R.id.菜单\图表->{
setCurrentFragment(chartFrag)
}
}
真的
}
}
fun setCurrentFragment(fragment:fragment)=supportFragmentManager.beginTransaction().apply{
替换(R.id.nav_主机_碎片,碎片)
提交()
}
fun getMonth():字符串{
val calendar=calendar.getInstance()
val simpleDateFormat=simpleDateFormat(“MMM-yyy”)
val date=simpleDateFormat.format(calendar.time)
返回日期
}
覆盖支持NavigateUp():布尔值{
返回navController.navigateUp()| | super.onSupportNavigateUp()
}
}
信息数据.kt
class MainActivity : AppCompatActivity() {
private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val dateView = findViewById<TextView>(R.id.dateTextView)
dateView.text = getMonth()
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_frag) as NavHostFragment
navController = navHostFragment.navController
setupActionBarWithNavController(navController)
val chartFrag = ChartsFragment()
val infoData = info_data()
val botNav = findViewById<BottomNavigationView>(R.id.botNavigation)
botNav.setOnNavigationItemSelectedListener {
when(it.itemId){
R.id.menu_statement ->{
setCurrentFragment(infoData)
}
R.id.menu_charts ->{
setCurrentFragment(chartFrag)
}
}
true
}
}
fun setCurrentFragment(fragment: Fragment) = supportFragmentManager.beginTransaction().apply{
replace(R.id.nav_host_frag, fragment)
commit()
}
fun getMonth():String{
val calendar = Calendar.getInstance()
val simpleDateFormat = SimpleDateFormat("MMM yyyy")
val date = simpleDateFormat.format(calendar.time)
return date
}
override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp() || super.onSupportNavigateUp()
}
}
class info_data : Fragment() {
val dataSet = dataSource().loadData()
val adapter = RecyclerViewAdapter(dataSet)
companion object{
var income = 0.0
var expense = 0.0
}
lateinit var navController: NavController
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_info_data, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val tvExpense = view.findViewById<TextView>(R.id.tvExpense)
tvExpense.text = "Expense : \n$ "+expense.toString()
val tvIncome = view.findViewById<TextView>(R.id.tvIncome)
tvIncome.text = "Income : \n$ "+income.toString()
val tvTotal = view.findViewById<TextView>(R.id.tvTotal)
tvTotal.text = "Total : \n$ "+grandTotal().toString()
val rv= view.findViewById<RecyclerView>(R.id.recyclerView)
rv.adapter = adapter
rv.layoutManager = LinearLayoutManager(activity)
val floatingBtn = view.findViewById<FloatingActionButton>(R.id.fabBtn)
floatingBtn.setOnClickListener(){
goToNextScreen(view)
}
}
fun goToNextScreen(view: View){
navController = Navigation.findNavController(view)
navController!!.navigate(R.id.action_info_data_to_form_data2)
}
fun grandTotal():Double{
val inc = income
val exp = expense
val total = inc-exp
return total
}
}
class-info\u数据:Fragment(){
val dataSet=dataSource().loadData()
val适配器=RecycleServiceAdapter(数据集)
伴星{
风险值收益=0.0
风险值费用=0.0
}
lateinit var navController:navController
覆盖创建视图(
充气器:布局充气器,容器:视图组?,
savedInstanceState:捆绑?
):查看{
//为该碎片膨胀布局
返回充气器。充气(R.layout.fragment\u info\u数据,容器,错误)
}
覆盖已创建的视图(视图:视图,保存状态:捆绑?){
super.onViewCreated(视图,savedInstanceState)
val tvExpense=view.findViewById(R.id.tvExpense)
tvExpense.text=“费用:\n$”+费用.toString()
val tvIncome=view.findViewById(R.id.tvIncome)
tvIncome.text=“收入:\n$”+收入.toString()
val tvTotal=view.findViewById(R.id.tvTotal)
tvTotal.text=“总计:\n$”+grandTotal().toString()
val rv=view.findViewById(R.id.recyclerView)
rv.适配器=适配器
rv.layoutManager=LinearLayoutManager(活动)
val floatingBtn=view.findViewById(R.id.fabBtn)
floatingBtn.setOnClickListener(){
goToNextScreen(视图)
}
}
趣味goToNextScreen(视图:视图){
navController=Navigation.findNavController(视图)
导航控制器!!.导航(R.id.action\u info\u data\u至\u form\u data2)
}
fun grandTotal():双倍{
val公司=收入
val exp=费用
val总计=inc exp
返回总数
}
}
**错误:
位于androidx.navigation.navigation.findNavController(navigation.java:84)
在com.example.myapplication.info_data.goToNextScreen上(info_data.kt:58)
在com.example.myapplication.info_data.onViewCreated$lambda-0(info_data.kt:53)**中,您使用的是
NavHostFragment
,这意味着您根本不应该执行碎片事务。有什么原因你不遵守规则吗?非常感谢。非常感谢。