【问题标题】:Setting action bar elevation programmatically throws NullPointer exception以编程方式设置操作栏高度会引发 NullPointer 异常
【发布时间】:2015-07-29 21:18:07
【问题描述】:

以编程方式设置操作栏的高度后,代码将以黄色突出显示,并引发以下警告。怎样才能消除此警告?

getSupportActionBar().setElevation(0);

警告

方法调用'getSupportActionBar().setElevation(0);'可能会产生'java.lang.NullPointerException'

【问题讨论】:

    标签: java android android-actionbar android-support-library android-elevation


    【解决方案1】:

    添加空检查:

    if(getSupportActionBar() != null) {
        getSupportActionBar().setElevation(0);
    }
    

    【讨论】:

      【解决方案2】:
      Try:
      
      
      ActionBar actionBar = getSupportActionBar();
      if(actionBar != null) {
         actionBar.setElevation(0);
      }
      

      【讨论】:

        猜你喜欢
        • 2014-07-11
        • 2013-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多