简体   繁体   中英

Linear Layout Cannot be cast to toolbar

I tried to add the shadow to toolbar following the method here https://stackoverflow.com/a/26904102/4273056

And it crashed on my device, these are the errors

I couldn't figure out how to post the code as the website keeps telling it is not formatted even after doing many things, so here is a paste bucket link..

The Errors are here,Click here

This is the toolbar layout

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">

<android.support.v7.widget.Toolbar
    style="@style/ToolBarStyle"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="@dimen/abc_action_bar_default_height_material"
    android:id="@+id/toolbar_actionbar"
    />

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- **** Place Your Content Here **** -->

    <View
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="@drawable/toolbar_dropshadow" />
</FrameLayout>

Main Activity

public class MainActivity extends ActionBarActivity implements NavigationDrawerCallbacks {

private Toolbar mToolbar;
private NavigationDrawerFragment mNavigationDrawerFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);



    mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer);
    mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public void onNavigationDrawerItemSelected(int position) {
    Toast.makeText(this, "Menu item selected -> " + position, Toast.LENGTH_SHORT).show();
}

@Override
public void onBackPressed() {
    if (mNavigationDrawerFragment.isDrawerOpen())
        mNavigationDrawerFragment.closeDrawer();
    else
        super.onBackPressed();
}

}

What should I do to get rid of these errors, please help

you need to change

mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);

to

mToolbar = (Toolbar) findViewById(R.id.tool_bar);

You should check in your activity_main.xml layout.

In this layout you are using an element (may be an include) with an

 id ="toolbar_actionbar"

You have to change this id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM