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
when you extend appcompatActivity then use
this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
and when you extend ActionBar then use
this.getActionBar().setDisplayHomeAsUpEnabled(true);
dont forget to call this function in oncreate after initializing the toolbar/actionbar
I think what you want to do is cast getActivity(). For example:
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
This is what you need to do with the new support libraries. AppCompatActivity has replaced ActionBarActivity.
When use AppCompatActivity must call
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Before getSupportActionBar()
public class PageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
this.getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
}
The Up Button is usually activated for Low-level Activities. In your manifest I only see the MainActivity.
I don't think it makes sense to activate the up button for the main activity.
Create an activity, then in the manifest add the parentActivityName attribute.
Then activate the up button on the activity's onCreate method.
This should help.
https://developer.android.com/training/appbar/up-action.html
In my case is because of styles.xml
set the wrong parent theme, i.e. NoActionBar
theme of course getSupportActionbar()
is null:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
Changed it to something else fixed it:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
For anyone else who has a BaseActivity, and a child that extends from it, make sure the super.onCreate() is called first before you do anything. The old Activity would work if you called super.onCreate() afterwards.
@Override
protected void onCreate(Bundle savedInstanceState)
{
getActionBar().setDisplayHomeAsUpEnabled(true);
super.onCreate(savedInstanceState);
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState); //do this first
getSupportActionBar().setDisplayHomeAsUpEnabled(true);