问题
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
回答1:
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
回答2:
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.
回答3:
To use AppCompat themes, your activity must extend AppCompatActivity (or deprecated ActionBarActivity), and to get action bar call getSupportActionBar() method.
来源:https://stackoverflow.com/questions/31222855/navigation-drawer-with-appcompat-and-normal-actionbar