1

I'm developing an app with Material Design.

After running the below code & tapping on 'About' option from the menu, the app is crashing & I'm running into the following exceptions:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abc.xyz/com.abc.xyz.AboutActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference`

Here's my AboutActivity.java file's code:

public class AboutActivity extends AppCompatActivity {

    public Toolbar toolbar;

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

        SpannableString s = new SpannableString("About");
        s.setSpan(new TypefaceSpan(this, "Pacifico.ttf"), 0, s.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle(s);
    }

    @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_about, 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_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

As I'm new to material design, I really don't know what to do here.

Tom
  • 16,842
  • 17
  • 45
  • 54
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – D. Ben Knoble Oct 03 '15 at 02:00

3 Answers3

0

Have you created the toolbar widget in your "activity_about.xml"?

<android.support.v7.widget.Toolbar
  android:id=”@+id/my_awesome_toolbar”
  android:layout_height=”wrap_content”
  android:layout_width=”match_parent”
  android:minHeight=”?attr/actionBarSize”
  android:background=”?attr/colorPrimary” />

Also you have a Toolbar toolbar; variable which is redundant. remove it if you are not using it.

Try: remove the call to getsupportactionbar.

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
toolbar.setHomeButtonEnabled(true);
Olivier Twist
  • 311
  • 2
  • 12
0

Generally, a null pointer exception simply indicates you are trying to access an item that isnt yet defined or initialised. Try this

ActionBar actionBar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);

You should paste you layout file here to provide more details

0

It's null because you are using toolbar in your layout (I see a Toobar object in your code). That means, probably your theme is a .NoActionBar theme. So, as you used Toolbar, you have to set that toolbar as ActionBar. Then you can use that ActionBar and apply whatever method you want to invoke. You can initialize the Toolbar with the following code snippet.

private void initToolbar() {
    final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    final ActionBar actionBar = getSupportActionBar();

    if (actionBar != null) {
        actionBar.setTitle("Your title");
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
}
ayon
  • 2,180
  • 2
  • 17
  • 32