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

后端 未结 24 1950
误落风尘
误落风尘 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:49

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

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

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

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

    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

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

    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.

    0 讨论(0)
提交回复
热议问题