0

I am currently trying to add a custom layout to my action bar but keep running into a null pointer exception and can't figure out a way around it.

Here is my onCreateOptionsMenu

    @Override
public boolean onCreateOptionsMenu(Menu menu)
{
    ActionBar mActionBar = getActionBar();
    LayoutInflater mInflater = LayoutInflater.from(this);

    View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
    TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.menuText);
    mTitleTextView.setText("Insert Title Here");

    ImageButton imageButton = (ImageButton) mCustomView.findViewById(R.id.menuPlusButton);
    imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Start New Course", Toast.LENGTH_LONG).show();
        }
    });

    mActionBar.setDisplayShowHomeEnabled(false);
    mActionBar.setDisplayShowTitleEnabled(false);
    mActionBar.setCustomView(mCustomView);
    mActionBar.setDisplayShowCustomEnabled(true);
    return super.onCreateOptionsMenu(menu);
}

The error seems to occur at mActionBar.setDisplayShowHomeEnabled(false) which indicates that ActionBar mActionBar = getActionBar() is returning null, and I cant figure out why. I extend AppCompatActivity and am running min API 7.

Thanks in advance.

1 Answer 1

4

Replace getActionBar() with getSupportActionBar() and change your imports to match.

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.