如何修复getActionBar方法可能会产生java.lang.NullPointerException [英] How to fix getActionBar method may produce java.lang.NullPointerException

查看:103
本文介绍了如何修复getActionBar方法可能会产生java.lang.NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用工具栏作为活动中的操作栏.我正在尝试将getActionBar().setDisplayHomeAsUpEnabled(true);方法添加到Activity.java文件中,以便在较旧的设备上进行向上导航.

I am using a toolbar as my actionbar in an activity. I am trying to add the method getActionBar().setDisplayHomeAsUpEnabled(true); to the Activity.java file for Up navigation for older devices.

该方法在Android Studio中产生以下错误消息:

The method produces the following error message in Android Studio:

方法调用可能会产生java.lang.NullPointerException

Method invocation may produce java.lang.NullPointerException

工具栏上的向上"导航在较新的设备上可以正常使用...现在,我正在尝试找出如何确保它在较旧的设备上可以使用的功能. 请告知.

The Up navigation on the toolbar works fine on newer devices...now I'm trying to figure out how to make sure it will work for older devices. Please advise.

来自build.gradle:

From build.gradle:

dependencies {
   compile "com.android.support:appcompat-v7:22.1.0"
}

来自AndroidManifest.xml:

From AndroidManifest.xml:

android:theme="@style/Theme.AppCompat.NoActionBar.FullScreen" 

来自styles.xml

From styles.xml

<style name="Theme.AppCompat.NoActionBar.FullScreen" parent="AppTheme">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>

来自Activity.java

from Activity.java

public class CardViewActivity extends AppCompatActivity {

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

    if (toolbar != null) {
        // Up navigation to the parent activity for 4.0 and earlier
        getActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationIcon(R.drawable.ic_action_previous_item);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

}

推荐答案

实际上,Android Studio不会向您显示错误消息",而只是警告.

Actually Android Studio isn't showing you an "error message", it's just a warning.

一些答案​​建议使用断言,Dalvik运行时默认已关闭断言,因此您必须实际开启它以实际执行某项操作.在这种情况下(断言已关闭),您实际上所做的只是在欺骗Android Studio而不向您显示警告.另外,我更不想在生产代码中使用断言".

Some answers propose the use of an assertion, Dalvik runtime has assertion turned off by default, so you have to actually turn it on for it to actually do something. In this case (assertion is turned off), what you're essentially doing is just tricking Android Studio to not show you the warning. Also, I prefer not to use "assert" in production code.

我认为您应该做的很简单.

In my opinion, what you should do is very simple.

if(getActionBar() != null){
   getActionBar().setDisplayHomeAsUpEnabled(true);
}

更新: 如果您使用的是操作栏的支持库版本,则应将getActionBar()替换为getSupportActionBar().

Update: In case you're using the support library version of the Action Bar, you should replace getActionBar() with getSupportActionBar().

if(getSupportActionBar() != null){
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

这篇关于如何修复getActionBar方法可能会产生java.lang.NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆