Android 添加后退按钮活动

Android 添加后退按钮活动,android,Android,我正在使用android 2.1,希望在Mapsavity中添加一个后退按钮。我在这页上试过什么[但应用程序崩溃。我已按照另一个论坛的建议将extends FragmentActivity替换为AppCompatActivity,但该应用程序仍然崩溃。我知道这与actionbar有关,因为如果我将其删除,该应用程序将正常工作。actionbar似乎为空。我被卡住并启动了应用程序sev多次。以下是错误: java.lang.RuntimeException: Unable to start ac

我正在使用android 2.1,希望在Mapsavity中添加一个后退按钮。我在这页上试过什么[但应用程序崩溃。我已按照另一个论坛的建议将extends FragmentActivity替换为AppCompatActivity,但该应用程序仍然崩溃。我知道这与actionbar有关,因为如果我将其删除,该应用程序将正常工作。actionbar似乎为空。我被卡住并启动了应用程序sev多次。以下是错误:

 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

一些事情。确保您的Activity扩展了AppCompatActivity,确保您使用的样式有一个操作栏(您在清单中设置了这个)

确保您使用的是正确的操作栏(ActionBar或SupportActionBar)

最后,添加onOptionsItemSelected方法,以便您的后退按钮知道要做什么

public boolean onOptionsItemSelected(MenuItem item) {

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

    return super.onOptionsItemSelected(item);
}

一些事情。确保您的Activity扩展了AppCompatActivity,确保您使用的样式有一个操作栏(您在清单中设置了这个)

确保您使用的是正确的操作栏(ActionBar或SupportActionBar)

最后,添加onOptionsItemSelected方法,以便您的后退按钮知道要做什么

public boolean onOptionsItemSelected(MenuItem item) {

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

    return super.onOptionsItemSelected(item);
}

你能在onCreate方法中发布代码吗?使用了什么主题?它有ActionBar吗?请发布
MapsActivity.onCreate()
,Manifest file and styles.xml。你能在onCreate方法中发布代码吗?使用了什么主题?它有ActionBar吗?请发布
MapsActivity.onCreate())
,Manifest file and styles.xml.没问题!如果有机会,您介意接受答案吗?没问题!如果有机会,您介意接受答案吗?
getActionBar().setDisplayHomeAsUpEnabled(true);
public boolean onOptionsItemSelected(MenuItem item) {

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

    return super.onOptionsItemSelected(item);
}