-1

There are many answers on the same issues. I tried all of them. Most of are pointing out about changing activity to ActionBarActivity and changing getAction to getSupportAction. But nothing worked for me.

Is there any solution for this issue?

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

public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setLogo(R.drawable.logo_share);
    getSupportActionBar().setDisplayUseLogoEnabled(true);
    setContentView(R.layout.activity_main);
  }
}

exception on setDisplayShowEnabled

user207421
  • 289,834
  • 37
  • 266
  • 440
Stephen Nollan
  • 103
  • 2
  • 9

6 Answers6

2

This is a common problem if you're starting using ActionBar.

Here the step to solving the problem:

  1. Check if your activity is using correct theme. Each activity declaration is in AndroidManifest.xml. Something like this:

    <activity
        android:name="com.yourpackage.MainActivity"
        android:label="@string/app_name"
        android:theme="Theme.AppCompat.Light.DarkActionBar">
    

    If you found something theme with @style like this:

    <activity
        android:name="com.yourpackage.MainActivity"
        android:label="MainActivity"
        android:theme="@style/AppTheme.NoActionBar">
    

    Then you need to check for style resource in your project.

  2. Check your activity layout.

    If you creating a project generated from Android Studio, you should have something like this:

    <android.support.design.widget.CoordinatorLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:app="http://schemas.android.com/apk/res-auto"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:fitsSystemWindows="true">
    
     <RelativeLayout
         android:layout_width="match_parent"
         android:layout_height="match_parent">
    
       <android.support.design.widget.AppBarLayout
           android:id="@+id/activity_app_bar_layout"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:theme="@style/AppTheme.AppBarOverlay">
    
         <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"/>
    
       </android.support.design.widget.AppBarLayout>
    
       <!--- Here Your Layout -->
    
    
     </RelativeLayout>
    

You will see there is a view android.support.v7.widget.Toolbar toolbar. It means that you need to bind the toolbar in your Activity. Something like this:

public class MainActivity extends AppCompatActivity {

  private Toolbar mToolbar;

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

    mToolbar = (ToolBar)findViewById(R.id.toolbar);

    // then you can set the toolbar
    setSupportActionBar(mToolbar);
  }
}
Rakshith Kumar
  • 833
  • 7
  • 19
ישו אוהב אותך
  • 22,515
  • 9
  • 59
  • 80
1

Before setDisplayHomeAsUpEnabled check getSupportActionBar() is null or not.

if(getSupportActionBar() != null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
sasikumar
  • 10,919
  • 2
  • 21
  • 42
1

To use SupportActionBar. You have to follow below step

  • Your Activity Theme is NoActionBar
  • Include android.support.v7.widget.Toolbar to your activty layout
  • Call setSupportActionBar before getSupportActionBar

And please remove setContentView(R.layout.activity_main); at the end of onCreate method.

Luc Le
  • 186
  • 7
1

add the toolbar in activity_main xml:

  <include
            android:id="@+id/toolbar"
            layout="@layout/layout_custom_toolbar" />

After add

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/MyCustomToolbarPopup"
app:theme="@style/MyCustomToolbar">

<!-- This is a centered title -->

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/toolbar_title"
        style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:textColor="@android:color/white"
        android:textSize="@dimen/font_18sp"
        android:text=""
        android:visibility="visible" />


</LinearLayout>

For Style:

    <!--to customise tool bar widgets-->
<style name="MyCustomToolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <!--color for the title of toolbar-->
    <item name="android:textColorPrimary">@android:color/white</item>
    <!--color for the toolbar icons-->
    <item name="android:textColorSecondary">@color/colorAccent</item>
    <!-- <item name="actionMenuTextColor">@color/transparent_white</item>-->
</style>


<!--Toolbar Popup-->
<style name="MyCustomToolbarPopup" parent="ThemeOverlay.AppCompat.Light">
    <!--color for the title of toolbar-->
    <item name="android:textColorPrimary">@color/color_576069</item>
    <!--color for the toolbar icons-->
    <item name="android:textColorSecondary">@color/color_576069</item>
</style>

and in on Create Method Set Up Tool Bar

    private void setupToolbar() {
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null)
        setSupportActionBar(toolbar);

    // Show menu icon

    ab = getSupportActionBar();
    ab.setTitle("");
    ab.setHomeAsUpIndicator(R.drawable.ic_action_name);
    ab.setDisplayHomeAsUpEnabled(true);
    ab.setDisplayShowHomeEnabled(true);
   TextView actionBarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);


}
Akash Shah
  • 15
  • 4
1

Here is the simplest ONE LINE solution.

I think you are getting below NullPointerException:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null object reference

EXPLANATION:

#. As you are using getSupportActionBar(), you have to set at least one AppCompat theme to your MainActivity that contains ActionBar.

For example:

You can use below themes as they contains ActionBar

Theme.AppCompat
Theme.AppCompat.Light 
Theme.AppCompat.Light.DarkActionBar

#. As per your comment, here is your styles.xml.

<resources> 
    <!-- 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> 
</resources>

SOLUTION:

Just set this AppTheme to your MainActivity in AndroidManifest.xml.

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

    ...............
    ....................
</activity>

FYI, I have tried with this and its working perfectly.

OUTPUT:

enter image description here

Hope this will help~

Ferdous Ahamed
  • 19,328
  • 5
  • 45
  • 54
0

use this theme for your activity:

        android:theme="@android:style/Theme.Holo.Light.DarkActionBar"
vivek mahajan
  • 515
  • 3
  • 16