【问题标题】:Bottom Navigation Tool Bar and Fragment底部导航工具栏和片段
【发布时间】:2018-12-26 07:31:13
【问题描述】:

我的项目中有两个底部导航。第一个底部导航工作正常,第二个底部导航工作不工作,得到ToolBar 不能为空错误消息。我正在使用kotlin 语言。在一个BottomNavigationActivity 中,我有一个活动和两个片段。我附上了下面的错误消息,并导入了所有必要的库。任何帮助表示赞赏。

导入的包

import android.os.Bundle
import android.support.design.widget.BottomNavigationView
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar

使用的语言

Kotlin Language used.

代码

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        restoreSaveInstanceState(savedInstanceState)
        setContentView(R.layout.activity_test)
        toolbar = findViewById(R.id.toolbar)
        bottomNavigation = findViewById(R.id.bottom_navigation)
        setSupportActionBar(toolbar)
        initBottomNavigation()
        initFragment(savedInstanceState)
    }

问题

ToolBar must not be null error message.

错误

java.lang.IllegalStateException: findViewById(R.id.toolbar) must not be null
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.IllegalStateException: findViewById(R.id.toolbar) must not be null

【问题讨论】:

  • activity_test中有工具栏吗?
  • 感谢@DKV 的帮助。您的评论解决了这个问题。
  • 解决问题了吗?
  • 是的,@DKV。感谢您的评论。
  • 它在你的片段中吗?

标签: android android-studio android-fragments kotlin


【解决方案1】:

下面的代码解决了这个问题。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:fitsSystemWindows="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:theme="@style/AppTheme"
    tools:context="test"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    >

【讨论】:

    【解决方案2】:

    你不需要在 kotlin 中使用 findViewById。可以直接获取id。

    【讨论】:

    • 这应该是评论。
    猜你喜欢
    • 2018-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多