Problems with setSupportActionBar (toolbar) in android studio

1

I am new to the world of mobile programming and I had a problem when creating a toolbar in the java code, this does not recognize the object created with the name toolbar in setSupportActionBar (toolbar), here is the code:

package com.example.jayrosalazar.platzigram.view;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toolbar;
import android.support.v7.app.ActionBar;

import com.example.jayrosalazar.platzigram.R;

public class CreatAccountActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_creat_account);
        showToolbar(getResources().getString(R.string.toobar_tittle_createaccount),false);
    }

    //Método para visualizar y crear un Toolbar
    public void showToolbar(String tittle, boolean upButton){
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar) //Declaramos un objeto tipo Toolbar y lo instanciamos
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle(tittle);
        getSupportActionBar().setDisplayHomeAsUpEnabled(upButton);
    }

}

Here is the layout in xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"
    >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>
    
asked by Jayro Salazar 27.12.2017 в 21:15
source

2 answers

0

Your problem is that you are using android.widget.Toolbar in the code and you should use android.support.v7.Toolbar , since you defined it in the xml:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"

You're also using the setSupportActionBar() method that takes a Toolbar object from the Android support library as a parameter.

    
answered by 28.12.2017 в 09:36
0

This is because in the bookstores that you import you are calling the library:

import android.widget.Toolbar;

And you should be calling the library that you declared in the XML of your layout, which is:

import android.support.v7.widget.Toolbar;

Replace the first import with the second and it should work, as long as you have not made any changes to the XML that should look like this:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay" />

Greetings!

    
answered by 18.06.2018 в 03:32