user4546546
user4546546

Reputation:

Navigation drawer with AppCompat and normal actionbar?

I have currently, a navigation drawer, a actionbar(no toolbar), and in the styles i defined as parent Theme.AppCompat.Light. I want to stay at actionbar, but when i use that parent, i become the following error:

"Attempt to invoke virtual method 'android.content.Context android.app.ActionBar.getThemedContext()' on a null object reference"

in that code:

mDrawerListView.setAdapter(new ArrayAdapter<String>(
            getActionBar().getThemedContext(),
            R.layout.navdrawer_item_row,
            android.R.id.text1,
            new String[]{
                    getString(R.string.title_section1),
                    getString(R.string.title_section2),
                    getString(R.string.title_section3),
            }));

Have anyone a solve suggestion to fix? Thanks in advance

Upvotes: 1

Views: 3984

Answers (3)

Desolator
Desolator

Reputation: 22759

None of the above worked for me. What actually worked is to remove: android:theme="...." from application xml tag in AndroidManifest.xml file. Hope this helps to save someone the struggle that I had.

This issue occurred when I created a Navigation Drawer Activity in Android Studio 2.3.3 with API 25.

Upvotes: 2

Psypher
Psypher

Reputation: 10829

You need to use getSupportActionBar() instead of getActionBar(). Change all occurrences of this in your code.

Below is the corrected code:

   ActionBar ab = getSupportActionBar();
   mDrawerListView.setAdapter(new ArrayAdapter<String>(
        ab.getThemedContext(),
        R.layout.navdrawer_item_row,
        android.R.id.text1,
        new String[]{
                getString(R.string.title_section1),
                getString(R.string.title_section2),
                getString(R.string.title_section3),
        }));

Dont forget to import android.support.v7.app.ActionBar

Also in styles change the parent to Theme.AppCompat.Light.DarkActionBar

Upvotes: 1

greenfrvr
greenfrvr

Reputation: 643

To use AppCompat themes, your activity must extend AppCompatActivity (or deprecated ActionBarActivity), and to get action bar call getSupportActionBar() method.

Upvotes: 0

Related Questions