java getActionBar().setDisplayHomeAsUpEnabled(true) 抛出 NullPointerException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27078476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:10:03  来源:igfitidea点击:

getActionBar().setDisplayHomeAsUpEnabled(true) throws NullPointerException

javaandroid

提问by novice20

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

这个问题在stackoverflow中被问了好几次,我都试过了。但不幸的是,两者都不适合我。

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:

我正在尝试实现两个活动之间的导航,作为学习 Android 应用程序开发的一部分。我的最小 SDK 和目标 SDK 版本分别是 11 和 21 (Android 5)。我在 AndroidManifest.xml 中的设置如下所示:

<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>

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:

我有两个活动:MainActivity 和 DetailActivity。当我单击 MainActivity 中的按钮时,应用程序成功打开了 DetailActivity。但是当我尝试使用以下代码启用后退按钮时,它返回一个 NullPointerExcepion:

getActionBar().setDisplayHomeAsUpEnabled(true);

My both the classes extend ActionBarActivity.

我的两个类都扩展了 ActionBarActivity。

In MainActivity.java:

在 MainActivity.java 中:

public class MainActivity extends ActionBarActivity {
...
}

In DetailActivity.java:

在 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
}

I have also tried changing the themes. For example, android:theme="@android:style/Theme.Holo.Light".

我也试过改变主题。例如,android:theme="@android:style/Theme.Holo.Light"

回答by CommonsWare

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

您是从ActionBarActivity. 因此,您需要使用getSupportActionBar(),而不是getActionBar()来获取 - 提供的appcompat-v7操作栏向后移植。

回答by Aida Isay

import v7:

导入 v7:

 import android.support.v7.app.ActionBar;

then in the onCreatemethod:

然后在onCreate方法中:

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

回答by Mahendra

Use this..

用这个..

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

instead of this - getActionBar().setDisplayHomeAsUpEnabled(true);

而不是这个 - getActionBar().setDisplayHomeAsUpEnabled(true);

it will work perfectly.

它会完美地工作。

回答by VenMia0

Summay: To make sure you won't get a NullPointerException. You need to:

总结:确保您不会收到 NullPointerException。你需要:

  1. Import the right library target to you minium SDK version.
  2. Use the right themes, just as the question owner said.
  1. 将正确的库目标导入您的最小 SDK 版本。
  2. 正如问题所有者所说,使用正确的主题。

But in my situation an if statement is necessary to solve my App from crash. By the way, I'm using an AppCompatActivity to hold my view fragment.

但是在我的情况下,需要一个 if 语句来解决我的应用程序崩溃的问题。顺便说一下,我正在使用 AppCompatActivity 来保存我的视图片段。

public onCreateView(LayoutInflater inflater, final ViewGroup container,Bundle savedInstanceState){
    View view = inflater.inflate(R.layout.list_fragment, container, false);
    ActionBar actionBar = getActivity().getActionBar();
    if (actionBar != null){
        actionBar.setDisplayHomeAsUpEnabled(true);
    }