1
My friends have a problem, I tried to understand how Alertdialog works, where the focus was to create two buttons to confirm the dialogue. The code I’m using is this:
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.telephony.SmsManager;
import android.widget.EditText;
import android.widget.Toast;
import java.util.ArrayList;
/**
* Created by turkojanownz on 06/07/16.
*/
public class Manager{
public static void Enviar(Context context, EditText[] Text, String opcao) {
if (Manager.CheckEditText(Text) == 0) {
Toast.makeText(context, "Preencha os campos, nome, telefone com DDD e a mensagem para enviar a mesma.", Toast.LENGTH_SHORT).show();
return;
}
if (Manager.CheckEditText(Text) == 1)
{
Toast.makeText(context, "Informe o DDD e o telefone, para continuar.", Toast.LENGTH_SHORT).show();
return;
}
if (Confirm(context)){
SmsManager smsManager = SmsManager.getDefault();
String Telefone = "12997040012";
String Mensagem = opcao + Text[0].getText().toString() + "\nRemetente: " + Text[1].getText().toString() + "\n" + Text[2].getText().toString();
ArrayList<String> smsEmPartes = smsManager.divideMessage(Mensagem);
smsManager.sendMultipartTextMessage(Telefone, null, smsEmPartes, null, null);
Toast.makeText(context, "Mensagem enviada com sucesso", Toast.LENGTH_LONG).show();
}
}
public static boolean Confirm(Context context) {
final boolean[] retorno = new boolean[1];
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setMessage("Deseja realmente enviar a mensagem?");
dialog.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface di, int arg) {
retorno[0] = true;
}
});
dialog.setNegativeButton("Não", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface di, int arg) {
retorno[0] = false;
}
});
dialog.setTitle("Confirmação");
dialog.show();
return retorno[0];
}
public static short CheckEditText(EditText[] Campos){
for (int i = 0; i<Campos.length; ++i){
if (Campos[i].getText().length() == 0){
return 0;
}
}
if (Campos[2].length() < 10) {
return 1;
}
return 2;
}
}
And when I perform this function I get the following exception:
07-06 17:36:58.545 8484-8484/louvaiaosenhorjesus.teste E/AndroidRuntime: FATAL EXCEPTION: main
Process: louvaiaosenhorjesus.teste, PID: 8484
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:343)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:312)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:277)
at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:80)
at android.support.v7.app.AlertController.installContent(AlertController.java:214)
at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:257)
at android.app.Dialog.dispatchOnCreate(Dialog.java:373)
at android.app.Dialog.show(Dialog.java:274)
at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:953)
at louvaiaosenhorjesus.teste.Manager.Confirm(Manager.java:58)
at louvaiaosenhorjesus.teste.Manager.Enviar(Manager.java:29)
at louvaiaosenhorjesus.teste.MainClick$1.onClick(MainClick.java:33)
at android.view.View.performClick(View.java:4791)
at android.view.View$PerformClick.run(View.java:19884)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5268)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
What I want to do is to display a dialog when the user clicks the send button, this dialog will ask a simple question to the user if he wants to confirm the action. If you know a way to create this dialogue and that it works.
What is the context you are passing to your function
Confirm(Context context)
?– Vitor Henrique
getBaseContext()
– David Christian Dias Ordone
Try to pass the
Activity
as a context.– Vitor Henrique