Dialog cannot be Applied

Asked

Viewed 80 times

0

I’m trying to use a dialog but the following message appears inserir a descrição da imagem aqui

Follows my code

package com.example.gustavo.vigilantescomunitarios;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

public class TabRua extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.tab_rua, container, false);
    }

    public void abrirMensagem(View view){
        final Dialog dialogMensagem = new Dialog(this);
    }

}

1 answer

3


Obs: The Dialog class is the base class for dialog boxes, but you should avoid instantiating Dialog directly. Instead, use one of the following subclasses:

Alertdialog, Datepickerdialog or Timepickerdialog

Recommendation that the Google does in the documentation of Android.

Context

Interface that contains global information about an application environment, is an abstract class whose implementation is provided by Android system, allows access to specific resources of app, in addition to calls to application-level operations, such as launch activities, broadcast and receiving intentions, etc.

Some Objects and Components needs a Context, you need to inform them what is happening.

You can use the method getActivity()

final Dialog dialogMensagem = new Dialog(getActivity());

Alertdialog

To use the class Alertdialog you will always make the class instance Alertdialog.Builder, but why? The class Alertdialog does not by default allow classes of different packages to instantiate it, however, it provides us with the internal and static class Builder who receives a Context as a parameter and which returns the object itself, that is, we can call more than one method at the same time. So if we try:

AlertDialog dialogo = new AlertDialog();

Will return a build error. Instead do:

AlertDialog.Builder dialogo = new AlertDialog.Builder(this);
// Utiliza o método setTitle() para definir um titulo
dialogo.setTitle("Meu diálogo");
// Utiliza o método setMessage() para definir uma mensagem
dialogo.setMessage("Mensagem do diálogo.");
// Para exibir, use o método show()
dialogo.show();

Or you can do all this in just one had:

new AlertDialog.Builder(this).setTitle("Meu diálogo").setMessage("Mensagem do diálogo.").show();

See the documentation to learn more about the class Alertdialog.Builder

  • Your answer would be appreciated if you explained why you made the mistake and why you can’t use this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.