setDisplayHomeAsUpEnabled

setDisplayHomeAsUpEnabled

本文介绍了getActionBar()。setDisplayHomeAsUpEnabled(真)抛出NullPointerException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是问几次计算器,我已经试过所有的人。但不幸的是没有为我工作。

This question is asked several times in stackoverflow and I have tried all of them. But unfortunately neither is working for me.

我想实现导航两项活动之间,为学习Android应用程序开发的一部分。我的铅丹SDK和目标SDK版本是11和21(安卓5),分别。我在AndroidManifest.xml中设置如下:

I am trying to implement the navigation between two activities, as part of learning Android app development. My minium SDK and target SDK versions are 11 and 21 (Android 5), respectively. My settings in AndroidManifest.xml are shown below:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.navigation"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
             >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".DetailActivity" >
        </activity>
    </application>
</manifest>

我有两个活动:MainActivity和DetailActivity。当我点击MainActivity一个按钮,该应用程序成功打开了DetailActivity。但是,当我试图让使用以下code中的返回按钮,它返回一个NullPointerExcepion:

I have two activities: MainActivity and DetailActivity. When I click a button in MainActivity, the app successfully opens the DetailActivity. But when I am trying to enable the back button by using the following code, it returns a NullPointerExcepion:

getActionBar().setDisplayHomeAsUpEnabled(true);

我的两个类扩展ActionBarActivity。

My both the classes extend ActionBarActivity.

在MainActivity.java:

In MainActivity.java:

public class MainActivity extends ActionBarActivity {
...
}

在DetailActivity.java:

In DetailActivity.java:

public class DetailActivity extends ActionBarActivity{

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

        getActionBar().setDisplayHomeAsUpEnabled(true); // returns null pointer
}

我也试图改变主题。例如,安卓主题=@安卓风格/ Theme.Holo.Light。

推荐答案

您是从 ActionBarActivity 继承。因此,你需要使用 getSupportActionBar(),不是 getActionBar(),以获得在 appcompat-V7 -supplied行动吧反向移植。

You are inheriting from ActionBarActivity. Hence, you need to use getSupportActionBar(), not getActionBar(), to get at the appcompat-v7-supplied action bar backport.

这篇关于getActionBar()。setDisplayHomeAsUpEnabled(真)抛出NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:45