工具栏setTitle()无法按预期工作

问题描述 投票:0回答:6

我尝试使用这个stackoverflow question和互联网上其他来源的建议方法来设置Toolbar的标题。但它没有用。它只是显示应用程序的名称。

我可以在代码中看到两个警告

Method invocation getSupportActionBar().setDisplayHomeAsUpEnabled(true) may produce java.lang.NullPointerException

Method invocation getActionBar().setTitle("Help") may produce java.lang.NullPointerException

以下是我正在尝试更改工具栏标题的活动类的代码。当我运行代码时,我没有看到任何错误,工具栏的标题也没有更改为“帮助”。

package com.myapp.myapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;

    public class HelpActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help);     
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getActionBar().setTitle("Help");
        getSupportActionBar().setTitle("Help");
    }
}

activity_help.xml

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

<android.support.v7.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

toolbar.xml

<android.support.design.widget.AppBarLayout android:id="@+id/appBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <!-- Toolbar is the actual app bar with text and the action items -->
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>

我知道我做错了什么。但我无法在这里找到错误。请帮我正确实现。

java android android-toolbar
6个回答
0
投票

使用工具栏方法:

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("Title");

或者只是setTitle(“帮助”);


0
投票

用这个

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);        
 toolbar.setTitle("Help");
 setSupportActionBar(toolbar);
 getSupportActionBar().setDisplayHomeAsUpEnabled(true);
 getSupportActionBar().setHomeButtonEnabled(true);
 getSupportActionBar().setDisplayShowTitleEnabled(true); 

0
投票

为什么要将工具栏包装在AppBarLayout中?我在我的活动中也和你一样,我没有使用以下警告:

toolbar.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:background="?attr/colorPrimary"
    app:layout_scrollFlags="scroll|enterAlways">

</android.support.v7.widget.Toolbar>

活动

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Title");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);

0
投票

在设置标题检查条件工具栏之前,no等于null

把你的代码放进去吧

如果(工具栏!= NULL){

}


0
投票

将Toolbar设置为setSupportActionBar(工具栏)后,它不会抛出nullPointerException。它是Android Studio lint,显示可能会发生。

getActionBar()可以创建NullPointerException。

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Help");

这就足够了。


0
投票
Toolbar toolbar = view.findById(R.id.toolbar)
setSupportActionBar(toolbar)

if (getSupportActionBar != null) {
    toolbar.setTitle("Hi dog");
}

在Android文档之后,您必须首先将工具栏传递给setSupportActionBar。因此,从Android 3.0(API级别11)开始,使用默认主题的所有活动都将操作栏作为应用程序栏。 setSupportActionBar的作用是什么?要使用工具栏,一旦分配它,请比较与null不同的内容,您可以为其提供所需的选项。

有关更多信息,请参阅此文档:https://developer.android.com/training/appbar/setting-up?hl=us-419

© www.soinside.com 2019 - 2024. All rights reserved.