EquiWare
EquiWare

Reputation: 127

Basic toolbar crashes app

I created a basic tool bar off the android website and it is crashing with a code it gives to put in the onCreate Section. the only widget in the program so far is that and it crashed with the .java code

home.java

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.os.Bundle;

public class Home extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    Toolbar my_toolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(my_toolbar);
}
}

xml

 <android.support.v7.widget.Toolbar
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

Upvotes: 1

Views: 83

Answers (2)

Rohan Shukla
Rohan Shukla

Reputation: 439

In your styles.xml put below code..

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

and in your AndroidManifest.xml write the theme as below

       <activity android:name=".Home"
        android:theme="@style/AppTheme.NoActionBar"></activity>

Upvotes: 0

Nabin Bhandari
Nabin Bhandari

Reputation: 16399

Make sure the theme of your activity Home does not have an ActionBar already. Check the manifest file to find out which theme is being used.

Also check the file styles.xml and make sure the the theme used by your activity does not have an ActionBar. Use something like this:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

Upvotes: 1

Related Questions