在操作栏中返回导航不起作用

时间:2014-12-23 01:52:41

标签: android

我尝试使用此来源在操作栏中制作后退按钮。但它并没有奏效。 这是来源。

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location_found);
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

此代码显示没有错误,但是当我在模拟器上测试它时,它强制关闭。 谁知道什么是错的?

这是日志聊天:

12-23 10:49:02.725: D/dalvikvm(693): GC_CONCURRENT freed 190K, 5% free 7100K/7431K, paused 4ms+105ms
12-23 10:49:33.345: D/AndroidRuntime(693): Shutting down VM
12-23 10:49:33.355: W/dalvikvm(693): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
12-23 10:49:33.405: E/AndroidRuntime(693): FATAL EXCEPTION: main
12-23 10:49:33.405: E/AndroidRuntime(693): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.okeschool/com.okeschool.ajax}: java.lang.NullPointerException
12-23 10:49:33.405: E/AndroidRuntime(693):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
12-23 10:49:33.405: E/AndroidRuntime(693):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-23 10:49:33.405: E/AndroidRuntime(693):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-23 10:49:33.405: E/AndroidRuntime(693):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-23 10:49:33.405: E/AndroidRuntime(693):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-23 10:49:33.405: E/AndroidRuntime(693):  at android.os.Looper.loop(Looper.java:137)
12-23 10:49:33.405: E/AndroidRuntime(693):  at android.app.ActivityThread.main(ActivityThread.java:4340)
12-23 10:49:33.405: E/AndroidRuntime(693):  at java.lang.reflect.Method.invokeNative(Native Method)
12-23 10:49:33.405: E/AndroidRuntime(693):  at java.lang.reflect.Method.invoke(Method.java:511)
12-23 10:49:33.405: E/AndroidRuntime(693):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-23 10:49:33.405: E/AndroidRuntime(693):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-23 10:49:33.405: E/AndroidRuntime(693):  at dalvik.system.NativeStart.main(Native Method)
12-23 10:49:33.405: E/AndroidRuntime(693): Caused by: java.lang.NullPointerException
12-23 10:49:33.405: E/AndroidRuntime(693):  at com.okeschool.ajax.onCreate(ajax.java:15)
12-23 10:49:33.405: E/AndroidRuntime(693):  at android.app.Activity.performCreate(Activity.java:4465)
12-23 10:49:33.405: E/AndroidRuntime(693):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-23 10:49:33.405: E/AndroidRuntime(693):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
12-23 10:49:33.405: E/AndroidRuntime(693):  ... 11 more

4 个答案:

答案 0 :(得分:1)

您的父活动有一些状态在您离开后会丢失。使用向上导航返回到它时,您将创建父活动的新实例。如果要重新使用旧实例及其状态,则应将android:launchMode="singleTop"添加到AndroidManifest.xml中的父活动

<activity
    android:name="com.example.app.ParentActivity"
    android:launchMode="singleTop">
</activity>
<activity
    android:name="com.example.app.ChildActivity"
    android:parentActivityName="com.example.app.ParentActivity" >
    <!-- Parent activity meta-data to support 4.0 and lower -->
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.app.ParentActivity" />
</activity>

更多信息
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode http://developer.android.com/guide/components/tasks-and-back-stack.html

答案 1 :(得分:0)

您是否在清单中定义了父活动?

选中此项:Link。我正在粘贴下面的内容以节省时间。

从Android 4.1(API级别16)开始,您可以通过在android:parentActivityName元素中指定<activity>属性来声明每个活动的逻辑父级。

如果您的应用支持Android 4.0及更低版本,请在您的应用中添加支持库,并在<meta-data>内添加<activity>元素。然后将父活动指定为android.support.PARENT_ACTIVITY的值,与android:parentActivityName属性匹配。

例如:

<application ... >
...
<!-- The main/home activity (it has no parent activity) -->
<activity
    android:name="com.example.myfirstapp.MainActivity" ...>
    ...
</activity>
<!-- A child of the main activity -->
<activity
    android:name="com.example.myfirstapp.DisplayMessageActivity"
    android:label="@string/title_activity_display_message"
    android:parentActivityName="com.example.myfirstapp.MainActivity" >
    <!-- Parent activity meta-data to support 4.0 and lower -->
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>

答案 2 :(得分:0)

试试这个。

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

在你的AndroidManifest帖子上发布。

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">

    <activity android:name=".MainActivity" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

    <activity android:name=".ActualActivity" android:parentActivityName=".MainActivity"/>
</application>

答案 3 :(得分:0)

如果您的minSdkVersion为11或更高,则始终会使用您在getActionBar()中调用的方法activity_location_found

尝试改为调用getSupportActionBar()方法。

这是你的代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_location_found);
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
}

试试这个:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_location_found);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

如果您按原样拆分声明:

ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);

确保选择正确的ActionBar classpublic abstract class ActionBar上有android.app;一个public asbstract class ActionBar引用android.support.v7.app

也许您使用错误的方法调用了ActionBar class