Alertdialog Show = New Alertdialog.builder(this) Is Undefined
Solution 1:
Do this:
AlertDialog.Builderbuilder=newAlertDialog.Builder(context);
builder.setTitle("Message")
.setMessage(str)
.setNeutralButton("OK", null);
AlertDialogdialog= builder.create();
dialog.show();
Instead of:
AlertDialogshow=newAlertDialog.Builder(this)
.setTitle("Message")
.setMessage(str)
.setNeutralButton("OK", null)
.show();
You have to create an instance of an AlertDialog.Builder
first. Then you can build the Dialog
with builder.create()
. Then you can show the Dialog
with .show()
.
Solution 2:
I'm new to android but found that sometimes you cant create an alert inside a class that is not an activity. you should then get the context directly and create the alert.
publicvoidshowAlert(String message){
AlertDialog.Builderbuilder=newAlertDialog.Builder(this.getContext());
builder.setTitle("Here is a message message from my activity")
.setMessage(message)
.setNeutralButton("OK", null);
AlertDialogdialog= builder.create();
dialog.show();
}
Solution 3:
You can't use dialog on BroadcastReceiver, so instead you better call an activity for the dialog box from the BroadcastReceiver,
add this code in your onReceive function :
@OverridepublicvoidonReceive(Context context, Intent intent)
{
Intenti=newIntent(context, {CLASSNAME}.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
fill the {CLASSNAME} with the dialog activity, heres my dialog activity :
package com.example.mbanking;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
// ALERT DIALOG// Sources : http://techblogon.com/alert-dialog-with-edittext-in-android-example-with-source-code/publicclassAlertDialogActivityextendsActivity
{
@OverrideprotectedvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
AlertDialog.Builderbuilder=newAlertDialog.Builder(this);
builder
.setTitle("Test")
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", newDialogInterface.OnClickListener()
{
publicvoidonClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
})
.setNegativeButton("No", newDialogInterface.OnClickListener()
{
publicvoidonClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});
AlertDialogalert= builder.create();
alert.show();
}
}
where I got the answer ?, here : How do you use an alert dialog box in a broadcast receiver in android? thanks to Femi !!, I just spread the news :D,
greetings from Indonesia.
Solution 4:
Write this code instead. I tested it.
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button btn=(Button)findViewById(R.id.btnLogin);
btn.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
ShowMessage();
}
});
}
protectedvoidShowMessage(){
AlertDialogshow=newAlertDialog.Builder(this)
.setTitle("Message")
.setMessage("khguh")
.setNeutralButton("OK", null)
.show();
}
Post a Comment for "Alertdialog Show = New Alertdialog.builder(this) Is Undefined"