0

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

Community
  • 1
  • 1
Sourabh
  • 172
  • 2
  • 5
  • 13

2 Answers2

2

you need to change

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

to

mToolbar = (Toolbar) findViewById(R.id.tool_bar);
Pedro Oliveira
  • 20,442
  • 8
  • 55
  • 82
  • Sorry I had posted the modified toolbar layout, changed it to original, the id is toolbar_actionbar.. – Sourabh Jan 28 '15 at 12:28
  • I dont get an error other than this, the app just crashes on the device – Sourabh Jan 28 '15 at 12:29
  • If that's really your current code (because you can't even copy paste the correct code to the answer) then there is no way it would generate a cast exception. Check your `activity_main` to see if that really matches the one you posted here. – Pedro Oliveira Jan 28 '15 at 12:33
  • It does, I have just included the toolbar layout to the main activity – Sourabh Jan 28 '15 at 12:35
  • What? You included what? That should be the content of `activity_main.xml`. Not a content of a included layout. Your `activity_main` should look like the one you posted......You don't need to `` nothing – Pedro Oliveira Jan 28 '15 at 12:36
  • The toolbar is included into the main layout, below the drawer layout. That is what I meant. There is nothing other than that.. Its just a plain main_activity.xml – Sourabh Jan 28 '15 at 12:38
  • I don't even know what you're doing anymore. What drawer layout? I don't see any drawer layout on your activity_main.xml.........Does your `` have any id? – Pedro Oliveira Jan 28 '15 at 12:40
0

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.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841