0

There are a number of questions dealing with this error. The solutions to these are:
- have the MainActivity class extend AppCompatActivity
- call getSupportActionBar(), not getActionBar()

I am already doing these.

I see a difference in appcompat-v7 22.1.1 and appcompat-v7 23.1.1. I get the error in 23. In 22 it works fine.

Stepping through the code in the debugger, I see that in AppCompatDelegateImplV7.intWindowDecorActionBar(), mHasActionBar is false, causing mActionBar to not be instantiated.

Stepping through the code in 22.1.1, the code is slightly different, but ultimately, I see that mHasActionBar is true and so the code works.

How do I deal with this?

import android.support.v7.app.ActionBar;
...
public class MainActivity extends AppCompatActivity  implements ...

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...    
    ActionBar actionBar = getSupportActionBar();

    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeAsUpIndicator(R.drawable.ic_drawer);
    ....
}

1 Answer 1

1

You might want to try to apply the NoActionBar theme to the application in the manifest, and use a Toolbar instead of Actionbar in the layout xml files.

Theme.AppCompat.Light.NoActionBar
  1. Adding the Tool bar to the layout xml file

    <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_player"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            style="@style/Topeka.TextAppearance.Title"
            android:background="@color/topeka_primary"
            android:elevation="@dimen/elevation_header">

Then in Java code, set it up as the Actionbar

 private void setUpToolbar(Player player) {
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_player);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    ((TextView) toolbar.findViewById(R.id.title)).setText(getDisplayName(player));
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.