I have a code module which implements viewpager with navigation drawer, however, when I run the code I get the following error
01-26 09:20:02.958: D/AndroidR
I know that this question is something old. But this can help many who present this problem.
To solve this problem, check if there is a point of nullity. Then apply the corresponding configuration:
if(getSupportActionBar() != null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
The best solution do this in your Oncreate method
ActionBar actionBar = getSupportActionBar();
if(actionBar != null){
actionBar.setDisplayHomeAsUpEnabled(true);
}
followed by a new class
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == android.R.id.home){
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
In my case I had the same error but my mistake was that I didn't declare my Toolbar.
So, before I use getSupportActionBar I had to find my toolbar and set the actionBar
appbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(appbar);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_nav_menu);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Try doing this:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
Instead of this:
actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
For those still having this issue, my issue was resolved in the AndroidManifest.xml file. Where it says <activity android:name=".MainActivity" android:theme="@style/AppTheme.NoActionBar">
, you need to remove NoActionBar
, making it <activity android:name=".MainActivity" android:theme="@style/AppTheme">
, because with NoActionBar set the app doesnt know whether or not it wants an action bar when you call one up inside of MainActivity.java
If anybody wants to use android.app.ActionBar and android.app.Activity you should change the app theme in styles.xml, for example:
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
The problem is you could be using an AppCompat theme.
On the other hand, if you want to use android.support.v7.app.ActionBar and you extend your activity with AppCompatActivity then you must use an AppCompat theme to avoid this issue, for example:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
Hope this helps.