0

I'm new to android. Been working with these action bars lately.

I used this following code on my mainActivity's onCreat()

  import android.support.v7.app.ActionBar;
  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setIcon(R.mipmap.ic_launcher_round);
    }

and it works But if I use this code

import android.ActionBar;
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setIcon(R.mipmap.ic_launcher_round);
    }

It creates a Null pointer exception at setDisplayShowHomeEnabled(). Due to that the getActionBar() returning Null.

Can anyone say the reason behind this.?

Thanks.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • Everything in the [accepted answer there](https://stackoverflow.com/a/26435242) also applies to `AppCompatActivity`, as that's likely what your `MainActivity` is actually extending. – Mike M. Aug 10 '17 at 06:42
  • 1
    Thank You @MikeM. – Sarath S Aug 10 '17 at 06:53

0 Answers0