Skip to content Skip to sidebar Skip to footer

Coordinatorlayout Cannot Be Cast To Android.support.v7.widget.toolbar

I am trying to use this Firebase authentication in my project, and I am getting an error that i can't figure out how to solve, and what causes it: My xml of the layout:

Solution 1:

Your CoordinatorLayout has the id 'toolbar'

<android.support.design.widget.CoordinatorLayout 
  ..
  android:id="@+id/toolbar"
  ..>

But in Java you cast it to a Toolbar object

Toolbartoolbar= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

This throws the exception because the View associated with the id R.id.toolbar is not a Toolbar.

To fix this, remove the above two lines from your Java code, since you do not actually have a Toolbar in your layout. You should probably also change the id for the CoordinatorLayout to something more descriptive such as "@+id/coordinator"


Edit: If I look at the example you are recreating, there is no Toolbar there. Layout Example And honestly for a login page it wouldn't make the most sense. However if you want to add it you could add this to your layout. Place it as the first child of the top LinearLayout:

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

But if you are not really confident with handling layouts and such, I would highly advice to read some more documentations and try simpler examples to get famillar with the platform. A good place to start is here: https://developer.android.com/guide/topics/ui/declaring-layout.html

Solution 2:

check your import modules carefully you should import this module

android.support.v7.widget.Toolbar

instead of this one

android.widget.Toolbar

Post a Comment for "Coordinatorlayout Cannot Be Cast To Android.support.v7.widget.toolbar"