简体   繁体   中英

Toolbar setTitle() not working as expected

I tried using the suggested methods from this stackoverflow question and other sources in the internet to set title of the Toolbar . But it is not working. It is just showing the app's name.

I can just see the two warnings in the code

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

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

Here is the code of the activity class where I am trying to change the title of the Toolbar. When I run the code, I don't see any errors and the title of the Toolbar is not changed to "Help".

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>

I know I am doing something wrong. But I am not able to find what is wrong here. Please help me acheive this correctly.

Use toolbar method:

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

or just setTitle("Help");

Use this

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

Why are you wrapping your Toolbar inside an AppBarLayout ? I do the same as you in my activity and I have no warning using the following:

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>

Activity

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

before setting the title check condition toolbar no equals to null

just put your code in

if(toolbar!=null){

}

Once you set Toolbar as setSupportActionBar(toolbar) it will not throw nullPointerException. It's Android Studio lint which is showing there might occur.

getActionBar() can create NullPointerException.

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

This would be enough.

Toolbar toolbar = view.findById(R.id.toolbar)
setSupportActionBar(toolbar)

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

Following the Android documentation, you must first pass your toolbar to setSupportActionBar. Therefore, as of Android 3.0 (API level 11), all activities that use the default theme have an action bar as an application bar. What does setSupportActionBar work for? To make use of the toolbar, once assigning it compare what is different from null and you can give it the options you need.

For more information follow this documentation: https://developer.android.com/training/appbar/setting-up?hl=us-419

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM