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

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

    I am a new to Android App development. I faced this error and spend almost 5 hours trying to fix it. Finally, i found out the following was the root cause for this issue and if anyone to face this issue again in the future, please give this a read.

    I was trying to create a Home Activitiy with a Video Background, for which i had to change the parent theme from the default setting of Theme.AppCompat.Light.DarkActionBar to Theme.AppCompat.Light.NoActionBar. This worked fine for the Home Activity, but when i set a new button with a onclicklistener to navigate to another Activity, where i had set a custom text to the Action Bar, this error is thrown.

    So, what i ended up doing was to create two themes and assigned them to the activities as follows.

    Theme.AppCompat.Light.DarkActionBar - for Activities with Action Bar (default)
    Theme.AppCompat.Light.NoActionBar - for Activities without Action Bar 
    

    I have made the following changes to make fix the error.

    1. Defining the themes in styles.xml

      <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
          <item name="colorPrimary">@color/colorPrimary</item>
          <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
          <item name="colorAccent">@color/colorAccent</item>
      </style>
      
      <style name="DefaultTheme" parent="Theme.AppCompat.Light.DarkActionBar">
          <item name="colorPrimary">@color/colorPrimary</item>
          <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
          <item name="colorAccent">@color/colorAccent</item>
      </style>
      
    2. Associating the Activities to their Respective Themes in AndroidManifest.xml

      <activity android:name=".Payment"
          android:theme="@style/DefaultTheme"/>
      
      <activity android:name=".WelcomeHome"
          android:theme="@style/AppTheme.NoActionBar">
      
    0 讨论(0)
  • 2020-11-28 22:39

    Please add after Toolbar

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().hide();
    
    0 讨论(0)
  • 2020-11-28 22:40

    Try this hope it will work, my code is works fine

    Toolbar toolbar = findViewById(R.id.toolbar1);
    setSupportActionBar(toolbar);
    
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setTitle("");
    
    0 讨论(0)
  • 2020-11-28 22:40

    I got the same error once, I created a template for a default toolbar( toolbar.xml) and then in another view I created a collapsable toolbar.

    I required to add the collapsable toolbar to a view that show some information of the user(like whatsapp), but the method findViewById was referencing the default toolbar(id toolbar), not the collapsable one( id toolbar as well) yes, I wrote the same id to both toolbar, so when I tried to access the activity the app crashed showing me the error.

    I fixed the error by changing the id of the collapsable toolbar to id:col_toolbar and the error gone away and my app worked perfectly

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

    If you are using android.app.ActionBar and android.app.Activity you should change the app theme in application tag:

    < application
         android:theme="@android:style/Theme.Holo.Light">
    
    0 讨论(0)
  • 2020-11-28 22:44

    if u have creat a new toolbar then apply check that u have apply the method setSupportActionBar(); it will defenetly help you

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