Android Error [Attempt to invoke virtual method 'void android.app.ActionBar' on a null object reference]

后端 未结 24 1948
误落风尘
误落风尘 2020-11-28 22:13

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         


        
相关标签:
24条回答
  • 2020-11-28 22:45

    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

    0 讨论(0)
  • 2020-11-28 22:45

    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.

    0 讨论(0)
  • 2020-11-28 22:45

    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);
    
        }
    }
    
    0 讨论(0)
  • 2020-11-28 22:47

    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

    0 讨论(0)
  • 2020-11-28 22:48

    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">
    
    0 讨论(0)
  • 2020-11-28 22:48

    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.

    Child extends Activity - could call super after you did stuff

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        getActionBar().setDisplayHomeAsUpEnabled(true);
        super.onCreate(savedInstanceState);
    

    Child extends AppCompatActivity - ya gotta call super first

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState); //do this first
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
    0 讨论(0)
提交回复
热议问题