【问题标题】:kotlin.TypeCastException: null cannot be cast to non-null type android.support.v7.widget.Toolbarkotlin.TypeCastException: null 不能转换为非 null 类型 android.support.v7.widget.Toolbar
【发布时间】:2017-08-27 02:47:03
【问题描述】:

我是Android 的新手。我的问题可能是什么情况?我正在尝试将我的片段呈现给MainActivity。任何建议都会有所帮助。谢谢

主要活动类...

class NavigationActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.fragment_schedule)

        val toolbar = findViewById(R.id.toolbar) as Toolbar
        setSupportActionBar(toolbar) // setup toolbar
        toolbar.setNavigationIcon(R.drawable.ic_map)

        val drawer = findViewById(R.id.drawer_layout) as DrawerLayout
        val toggle = ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
        drawer.addDrawerListener(toggle) // navigation drawer
        toggle.syncState()

        val navigationView = findViewById(R.id.nav_view) as NavigationView
        navigationView.setNavigationItemSelectedListener(this) //setup navigation view

}

我的片段类..

 class fragment_schedule : Fragment() {
 override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
   // Inflate the layout for this fragment
   return inflater!!.inflate(R.layout.fragment_schedule, container, false)
}

【问题讨论】:

  • 可能使用了错误的布局文件。看看这条线:setContentView(R.layout.fragment_schedule)

标签: android fragment kotlin


【解决方案1】:

另外一个原因可能是您将 setContentView 放在导致此异常的项目下方。至少我是这样把它固定在我身边的。

【讨论】:

    【解决方案2】:

    Sanislondra 发现我收到此异常的原因是 xml 中的 navigation graphs id 应该与底部导航菜单 xml 中的 menu itemsid 匹配。

    【讨论】:

      【解决方案3】:

      您的布局显然没有 ID 为 R.id.toolbar 的工具栏组件,这就是为什么 findViewById(...) 返回无法转换为 Toolbarnull

      Null 安全性在 Kotlin 的一部分中很重要。它可以帮助您在开发的早期阶段揭示大多数 NPE。在官方Kotlin 网站上阅读more about null safety

      如果工具栏组件的存在是必不可少的,那么永远不要让val toolbar 变量为空。

      在这种情况下,您当前的代码是正确的,请不要更改它。而是更正您的布局/错误的工具栏 ID 问题。

      var toolbar: Toolbar? = null 定义了 Toolbar 类型的可空变量,如果您的活动布局中没有工具栏组件,则不会引发异常,但是如果您希望真正拥有该组件,您将得到其他异常让您或应用用户感到惊讶的程序稍后甚至不可预测的行为

      另一方面,您应该在使用可为空变量时进行空安全检查

      在这种情况下,您的代码应稍作更改。重要部分:

      val toolbar = findViewById(R.id.toolbar) as Toolbar?
      

      ? 表示如果findViewById 返回null,那么这个null 将被分配给val toolbar 而不是上升kotlin.TypeCastException: null cannot be cast to non-null type android.support.v7.widget.Toolbar 异常

      完整代码块示例:

       val toolbar = findViewById(R.id.toolbar) as Toolbar? // toolbar now is nullable 
      
       if(toolbar != null) {
       // kotlin smart cast of toolbar as not nullable value
              setSupportActionBar(toolbar) // setup toolbar
              toolbar.setNavigationIcon(R.drawable.ic_map)
      
              val drawer = findViewById(R.id.drawer_layout) as DrawerLayout
              val toggle = ActionBarDrawerToggle(
                      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
              drawer.addDrawerListener(toggle) // navigation drawer
              toggle.syncState()
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-09
        相关资源
        最近更新 更多