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

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

    in this line in your activity:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.Activity_Main);
    

    use this:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.*);
    

    * is your activity

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

    If you encounter this error

    "java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle (java.lang.CharSequence)' on a null object reference. "
    

    Just use

    setSupportActionBar (toolbar). 
    
    0 讨论(0)
  • 2020-11-28 22:57

    Try to check here

    res >> values >> styles.xml

    make sure that there no code like this

    <item name="windowActionBar">false</item>
    

    if there are code like that, you can disable for a while, or erase it

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

    You should try this one. I think it will work.

        Toolbar toolbar = findViewById(R.id.toolbar1);
        setSupportActionBar(toolbar);
    
        mDrawerLayout = findViewById(R.id.drawer_layout);
        mDrawerLayout = findViewById(R.id.drawer_layout);
        mDrawerLayout.setDrawerShadow(R.drawable.rectagle_with_black_outer,
                GravityCompat.START);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
    
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                toolbar, R.string.navigation_drawer_close,
                R.string.navigation_drawer_close) {
            public void onDrawerClosed(View view) {
                invalidateOptionsMenu();
            }
    
            public void onDrawerOpened(View drawerView) {
                invalidateOptionsMenu();
            }
        };
    
    0 讨论(0)
  • 2020-11-28 23:03

    Your code is throwing on com.example.tabwithslidingdrawer.MainActivity.onCreate(MainActivity.java:95):

            // enabling action bar app icon and behaving it as toggle button
            getActionBar().setDisplayHomeAsUpEnabled(true);
            getActionBar().setHomeButtonEnabled(true);
    

    The problem is pretty simple- your Activity is inheriting from the new android.support.v7.app.ActionBarActivity. You should be using a call to getSupportActionBar() instead of getActionBar().

    If you look above around line 65 of your code you'll see that you're already doing that:

            actionBar = getSupportActionBar();
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
            // TODO: Remove the redundant calls to getSupportActionBar()
            //       and use variable actionBar instead
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
    

    And then lower down around line 87 it looks like you figured out the same:

            getSupportActionBar().setTitle(
                            Html.fromHtml("<font color=\"black\">" + mTitle + " - "
                                            + menutitles[0] + "</font>"));
            // getActionBar().setTitle(mTitle +menutitles[0]);
    

    Notice how you commented out getActionBar().

    0 讨论(0)
  • 2020-11-28 23:04

    Whenever such an error occurs. Try to check Following Things

    1. Check what kind of Activity is being used, is it a simple android.app Activity or an AppCompatActivity or an ActionBarActivity and so on.

    2. Check if your activity type which is extended falls under the compat category

    example android.app based Activity/Fragment are non appCompat types, whereas android.support.v4.app.Fragment or android.support.v4.app.ActivityCompat are appCompat based

    if it falls under appCompat we use getSupportActionBar() else for android.app types we can use getActionBar()

    1. Check the theme applied to the activity in question in the manifest file

    example: In the manifest file if theme applied is say android:theme="@android:style/Theme.Holo.Dialog" getActionBar() will work

    but if theme applied for the activity in the manifest is as follows android:theme="@style/Theme.AppCompat.Light" then you have to use getSupportActionBar()

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