Desmond A
Desmond A

Reputation: 113

kotlin.TypeCastException: null cannot be cast to non-null type android.support.v7.widget.Toolbar

I'm new to Android here. What can be the situation for my problem? I'm trying to present my fragment to the MainActivity. Any suggestions will help. Thanks

Main Activity Class...

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

}

My Fragment Class..

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

Upvotes: 5

Views: 12550

Answers (3)

Rethinavel
Rethinavel

Reputation: 3952

Sanislondra figured out that the reason why I was getting this exception because the navigation graphs id in the xml should match the id of the menu items in the bottom navigation menu xml.

Upvotes: 1

Antonin GAVREL
Antonin GAVREL

Reputation: 11259

Also another reason could be that you had put setContentView below the item that caused this exception. At least it is how I fixed it on my side.

Upvotes: 1

Клаус Шварц
Клаус Шварц

Reputation: 3268

Your layout apparently has no toolbar component with id R.id.toolbar, that's why findViewById(...) returns null that can't be cast to Toolbar.

Null-safety is important built in part of Kotlin. It helps you to reveal most of NPEs on the early stages of development. Read more about null safety on official Kotlin site.

If presence of toolbar component is essential, then never make val toolbar variable nullable.

In this case your current code is correct, don't change it. Instead correct your layout/wrong toolbar id problem.

var toolbar: Toolbar? = null defines nullable variable of type Toolbar and no exception will be risen if there will be no toolbar component in your activity layout, but if you expect to really have this component, you will get other exceptions later or even unpredictable behaviour of your program that will surprise you or app users.

On the other hand you should make null-safety checks while using nullable variables

In that case your code should be slightly changed. Important part:

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

? means that if findViewById returns null, then this null would be assigned to val toolbar instead of rising kotlin.TypeCastException: null cannot be cast to non-null type android.support.v7.widget.Toolbar exception

Full code block example:

 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()
}

Upvotes: 0

Related Questions