0

I am using android 2.1 and wish to add a back button to a MapsAvtivity. I have tried what on this page [Display back button on action bar but the application crashes. I have replaced extends FragmentActivity with AppCompatActivity as was recommended in another forum but the application still crashes. I know it has something to do with the actionbar because if I remove it the application works. It appears that the actionbar is null. I am stuck and have started the application several times. Here is the error:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pc.canda.theplacestosee/com.pc.canda.theplacestosee.MapsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:144)
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                        at android.os.Looper.loop(Looper.java:135)
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                        at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
                                                                                     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference 
                                                                                        at com.pc.canda.theplacestosee.MapsActivity.onCreate(MapsActivity.java:31)
                                                                                        at android.app.Activity.performCreate(Activity.java:5937)
                                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:144)
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                        at android.os.Looper.loop(Looper.java:135)
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                        at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694
Community
  • 1
  • 1
kyzen
  • 293
  • 1
  • 3
  • 15

1 Answers1

3

A few things. Make sure your acttivity extends AppCompatActivity, make sure the style you are using has an action bar (you set this in the manifest).

android:theme="@style/AppTheme"

Make sure you are using the correct action bar (ActionBar or SupportActionBar)

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

or

getActionBar().setDisplayHomeAsUpEnabled(true);

Finally, add the onOptionsItemSelected method so that your back button knows what to do.

public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            return true;
    }

    return super.onOptionsItemSelected(item);
}
Jay
  • 324
  • 2
  • 14