kotlin.TypeCastException: null cannot be cast to non-null type android.support.v7.widget.Toolbar
10,866
Solution 1
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.
Solution 2
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.
Related videos on Youtube
Author by
Desmond A
Updated on July 10, 2022Comments
-
Desmond A almost 2 years
I'm new to
Android
here. What can be the situation for my problem? I'm trying to present my fragment to theMainActivity
. Any suggestions will help. ThanksMain 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) }