-4

In my project, I am trying to change the ActionBar title with getSupportActionBar().setTitle(), but I am keep getting the NullPointerException.

Error :

08-02 06:58:48.645 16441-16441/? E/AndroidRuntime: FATAL EXCEPTION: main
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime: Process: com.mynotescode.apps.layout, PID: 16441
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mynotescode.apps.layout/com.mynotescode.apps.layout.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2326)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:147)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5264)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at com.mynotescode.apps.layout.MainActivity.onCreate(MainActivity.java:18)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5975)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:147) 
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281) 
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5264) 
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900) 
08-02 06:58:48.645 16441-16441/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695) 

This is my MainActivity.java :

package com.mynotescode.apps.layout;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBar actionBar = getSupportActionBar();

        actionBar.setTitle("Hotel Hilton");
        actionBar.setSubtitle("Isla Nublar");
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_bookmark) {
            return true;
        }

        if (id == android.R.id.home){
            finish();
        }

        return super.onOptionsItemSelected(item);
    }
}

Please help me. Thank you.

Varundroid
  • 8,802
  • 13
  • 61
  • 90
  • Can you share your styles.xml file? I have a feeling that you are using a NoActionBar theme. – Varundroid Aug 02 '16 at 00:24
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – TheAnonymous010 Aug 02 '16 at 01:00

3 Answers3

3

First, this issue has nothing to do with Android Studio, Second, make sure you aren't using any theme that has NoActionBar at the end in your styles.xml, for instance - Theme.Light.NoActionBar.

Here is an example -

NO

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
</style>

YES

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>

Let me know, if it helped.

Varundroid
  • 8,802
  • 13
  • 61
  • 90
  • why is that? What if I have multiple different styles and one of them is NoActionBar? You do not explain why this is wrong – James Ele Jan 02 '21 at 18:03
0

Try to use toolbar instead of action bar like this, this is an example from an app Im making:

// Set your content view before this point.
Toolbar toolBar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolBar);
ActionBar actionBar = getSupportActionBar();
// Call any actionbar methods you like at this point!
actionBar.setHomeAsUpIndicator(R.drawable.back_home_ind);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setTitle((Html.fromHtml("<font color=\"#0288D1\">" + friendName + "</font>")));//By default set the color to grey

Also worth noting, if you have a Toolbar in your layout you will need to set it as the actionbar before calling getSupportActionBar(), if you have a Toolbar in your layout already and are using a noActionBar theme you will have to follow this example.

Dr. Nitpick
  • 1,446
  • 1
  • 10
  • 16
Andrey
  • 1,681
  • 1
  • 17
  • 37
  • That's a hack and not a solution. – Varundroid Aug 02 '16 at 00:31
  • If you are using a toolbar this is the correct way to set the title, [according to the docs](https://developer.android.com/training/appbar/setting-up.html#add-toolbar) you should not need the try catch around the logic to customize the action bar. I will submit an edit to remove that. – Dr. Nitpick Aug 02 '16 at 03:32
0

The reason generate NullPointerException is you lack something. In you code, you must be lack actionBar. Although your MainActivity extents AppCompatActivity, if in your XML file use NoActionBar theme like Theme.AppCompat.Light.NoActionBar, you will generate NullPointerException.