首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >以编程方式更改操作栏颜色可能会导致nullPointerException?

以编程方式更改操作栏颜色可能会导致nullPointerException?
EN

Stack Overflow用户
提问于 2015-08-18 12:30:50
回答 1查看 715关注 0票数 0

我试图使用以下代码更改Android应用程序中操作栏的颜色:

代码语言:javascript
运行
复制
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.background_actionbar)));

然而,这给出了一个警告,内容如下:

方法调用'getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.backgr...‘可能会产生'java.lang.NullPointerException‘

有什么办法解决这个问题吗?

注意:我以编程方式更改颜色,因为通过XML主题/样式更改它不起作用。

使用最小SDK 16。

在Android4.4.4设备上进行测试。

EN

Stack Overflow用户

发布于 2015-08-18 12:47:31

是的,如果您在NoActionBar中使用主题,那么您将得到NullPointerException

试试这个:

代码语言:javascript
运行
复制
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // The Action Bar is a window feature. The feature must be requested
    // before setting a content view. Normally this is set automatically
    // by your Activity's theme in your manifest. The provided system
    // theme Theme.WithActionBar enables this for you. Use it as you would
    // use Theme.NoTitleBar. You can add an Action Bar to your own themes
    // by adding the element <item name="android:windowActionBar">true</item>
    // to your style definition.
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

    setContentView(R.layout.main);

    // experiment with the ActionBar 
    ActionBar actionBar = getActionBar();
    actionBar.setBackgroundDrawable(new  ColorDrawable(getResources().getColor(R.color.background_actionbar)));
        //actionBar.hide();
}

您可以使用Toolbar

toolbar.xml

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
android:background="@color/light_blue">
</android.support.v7.widget.Toolbar>

将它包含在活动的布局中:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <include layout="@layout/toolbar" />
    </LinearLayout>
</RelativeLayout>

使用此代码进行活动:

代码语言:javascript
运行
复制
public class YourActivity extends AppCompatActivity {
    private Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout);

        // Set a Toolbar to replace the ActionBar.
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        //toolbar.setTitle("Setting");
    }

    public void setSupportActionBar(@Nullable Toolbar toolbar) {
        getDelegate().setSupportActionBar(toolbar);
    }
}
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32072510

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档