Dialog can not be applied

0

I'm trying to use a Dialog but the following message appears

Follow 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);
    }

}
    
asked by anonymous 07.10.2017 / 00:02

1 answer

3
  

Note: 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 #

     

Recommendation Google itself in documentation of Android .

Context

  

Interface that contains global information about an application environment, is an abstract class whose implementation is provided by the Android System , it allows access to application-specific features as well as calls to application-level operations such as launch activities, transmission and receipt intentions, etc.

Some Objects and Components need a Context , you need to inform them what is going on.

You can use the method getActivity()

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

AlertDialog

To use the AlertDialog class you will always do the AlertDialog.Builder class instance, but why? The AlertDialog class does not by default allow classes from different packages to instantiate it, but it gives us the built-in static class that gets a Context as parameter and that returns the object itself, that is, we can call more than one method at a time. So if you try:

AlertDialog dialogo = new AlertDialog();

Will return a compilation 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 documentation to learn more about the class AlertDialog.Builder

    
07.10.2017 / 00:21