Issue with AlertDialog - java

I've just implemented an AlertDialog into a fragment within my Android app and it is causing my application to crash when it is shown.
Any ideas on why this might be?
Dialog
void addSiteOption() {
String[] options = {"Auto", "Manual"};
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity().getApplicationContext());
builder.setTitle("Add");
builder.setMessage("Auto add - download. \n Manually add - no internet connection.");
builder.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int selectionIndex) {
switch (selectionIndex)
{
case 0:
break;
case 1:
break;
}
}
});
builder.show();
}
Error:
E/AndroidRuntime: FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: Resource ID #0x0

You are getting Application context here but you need to get the calling activity's context.So change your code
From this:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity().getApplicationContext());
To this:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

Context=container.getContext();
private void showAlert() {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Are you sure to clear history?");
builder.setPositiveButton("sure", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}

Related

intent in alart dialog [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
intent in alart dialog how pass in next intent
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String title = editTextTitle.getText().toString().trim();
String r=android.get(i).getPassword();
if(title.equals(r)){
view.getContext().startActivity(new Intent(activity,DetailView.class));
Toast.makeText(view.getContext()," user name " , Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(view.getContext()," user name not valid " , Toast.LENGTH_LONG).show();
}
// sendMessage(user.getId(), title, message);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
}
you need to use activity or context, for example, please refer below code
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity)
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
activity.startActivity(new Intent(activity, DetailView.class));
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
you can get activity or context from the contractor.
Change this part of the code:
view.getContext().startActivity(new Intent(activity,DetailView.class));
To this:
Intent intent = new Intent(FirstActivity.this,Screen2Activity.class);
startActivity(intent);
Full code:
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String title = editTextTitle.getText().toString().trim();
String r=android.get(i).getPassword();
if(title.equals(r)){
Intent intent = new Intent(FirstActivityName.this,Screen2ActivityName.class);
startActivity(intent);
Toast.makeText(view.getContext()," user name " , Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(view.getContext()," user name not valid " , Toast.LENGTH_LONG).show();
}
// sendMessage(user.getId(), title, message);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
}

I want to dismiss the dialog box as soon as it is connected to internet

Here I want to show two dialog boxes...one for if there is net connection available and other if there is no connection..but i want that when one dialog box is shown, the other dialogue box should be dismissed .......dismiss() is not working in this case....and somehow if I use AlertDialog instead of AlertDialog.Builder to use dismiss(), then i am not able give setPositive, setNegative and setNeutral buttons....any help will be appreciated.......
BroadcastReceiver br;
#Override
protected void onCreate(Bundle savedInstanceState) {
...........//
getStarted();
}
private void getStarted() {
if (br == null) {
br = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
...............//
if (state == NetworkInfo.State.CONNECTED) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setCancelable(false);
builder1.setTitle("Connected");
builder1.setMessage("Online");
builder1.setNeutralButton("Exit", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//
}
});
builder1.show();
}
else {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(false);
builder.setTitle("No Internet ");
builder.setMessage("Offline");
builder.setNeutralButton("Exit", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//
}
});
builder.show();
}
}
};
final IntentFilter if = new IntentFilter();
if.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
getActivity().registerReceiver(br, if);
}
}
}
Dismiss Your dialog if NetworkInfo.State.CONNECTED is connected,Please change builder1.show(); into builder1.dismiss();
if (state == NetworkInfo.State.CONNECTED) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setCancelable(false);
builder1.setTitle("Connected");
builder1.setMessage("Online");
builder1.setNeutralButton("Exit", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//
}
});
builder1.dismiss();
}
Use broadcast receiver to react when the connection is changed with intent filter android.net.ConnectivityManager.CONNECTIVITY_ACTION. So, you can do your stuffs when the receiver receive the intent (or there connection is changed). See here.

Alert Dialog with EditText closes if input is empty

I have alert dialog with edit text. User must enter something and if not and then system should alert him that this input is required.
This is code for Alter Dialog:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Razlog storniranja?");
// Set up the input
final EditText input = new EditText(this);
// Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
input.setInputType(InputType.TYPE_CLASS_TEXT);
input.setSingleLine(false);
input.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);
builder.setView(input);
// Set up the buttons
builder.setPositiveButton("Spremi", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("Otkaži", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
if (m_Text.length()==0)
{
input.setHint("Morate upisati razlog storniranja kartice (naloga)!");
input.setError("Upišite razlog storniranja!");
}
}
});
builder.show();
But when user click on positive button and input is empty the dialog is closed. Is something missing in my code to avoid closing dialog?
EDIT
I think I found solution. The last line builder.show(); should be deleted and instead, this piece of code should be added:
builder.setView(input);
final AlertDialog alertDialog = builder.create();
alertDialog.show();
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Boolean wantToCloseDialog = (input.getText().toString().trim().isEmpty());
// if EditText is empty disable closing on possitive button
if (!wantToCloseDialog) {
input.setHint("Morate upisati razlog storniranja kartice (naloga)!");
input.setError("Upišite razlog storniranja!");
alertDialog.dismiss();
}
}
});
I hope this will help others.
Well, I do not have any clue of what you are trying to do.
The error that I had when using your code was:
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
m_Text needs to be initialized.
in case m_Text is not initialized, on the onDismiss method you might need to change:
if (m_Text != null && m_Text.length()==0){ //this line
input.setHint("Morate upisati razlog storniranja kartice (naloga)!");
input.setError("Upišite razlog storniranja!");
}
Regarding the cancelable, you need to builder.cancelable(false); and whenever needed, builder.dismiss().
builder.setPositiveButton("Spremi", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
m_Text = input.getText().toString();
if (m_Text.isEmpty()) {
input.setHint("Morate upisati razlog storniranja kartice (naloga)!");
input.setError("Upišite razlog storniranja!");
input.setFocusable(true);
}else{
dialog.dismiss();
}
}
});
Use TextUtils.isEmpty() to check the empty string.
Try this:
builder.setPositiveButton("Spremi", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if(TextUtils.isEmpty(input.getText().toString())) {
// Empty
input.setError("Input is required!");
} else {
// Do something...
}
}
});
Hope this will help~

AlertDialog.Builder helper class cannot resolve constructor

I've built a helper class to store various AlertDialog types. I thought it would be helpful so that I could call them anywhere in my code. Unfortunately I get an error below at new AlertDialog.Builder(). It says Cannot resolve constructor `Builder(). How can I get this to work?
public class AlertDialogHelper {
public void showAboutDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder();
builder.setTitle(R.string.about);
builder.setMessage("A weather app made by Martin Erlic")
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Ok
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
In my activity:
private void showAboutAlertDialog() {
AlertDialogHelper alertDialogHelper = new AlertDialogHelper();
alertDialogHelper.showAboutDialog();
}
You should pass a Context in the constructor like this:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
From your activity:
alertDialogHelper.showAboutDialog(this);
now:
public void showAboutDialog(Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
....
I did it like this:
public class AlertDialogHelper {
public static Dialog CreateDialog(Context mContext) {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle(R.string.about);
builder.setMessage("A weather app made by Martin Erlic")
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Ok
}
});
return builder.create();
}
}
In my activity:
AlertDialogHelper.CreateDialog(this).show();

calling dialog from another dialog repeatedly in amy android app

case 2: new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_info)
.setTitle("Socket9 Registeration")
.setMessage("You have been Registered Successfully.Please Login to continue.")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() { #Override
public void onClick(DialogInterface dialog, int which) {
etMobileNo.setText("");
etPassword.setText("");
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
etMobileNo.setText("");
etPassword.setText("");
}
}).show();
break;
case 3: new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Socket9 Registeration")
.setMessage("Your Code doesn't match.Try Again")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() { #Override
public void onClick(DialogInterface dialog, int which) {
dismissDialog(2);
showDialog(4);
}}).show();
break;
case 4: new AlertDialog.Builder(this)
.setTitle("Enter Your Registeration Code")
.setView(input)
.setMessage("Registeration code has been delivered on your registered number via sms")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() { #Override
public void onClick(DialogInterface dialog, int which) {
String value = input.getText().toString().trim();
String regsCode2=etFake.getText().toString().trim();
System.out.println("Va "+value+" Reg"+regsCode2);
if(value.compareToIgnoreCase(regsCode2)==0){
validCodeMatch=objCommonServices.sendEvalidCode(etMobileNo.getText().toString().trim(), etPassword.getText().toString().trim(),"OK");
if(validCodeMatch.contains("Code Match")){
showDialog(2);
}
}
else{
dismissDialog(4);
showDialog(3);
}
}}).show();
I have created 3 dialogs tand calling each other on each user input every timeI am also trying to dismissDialog method because earlier i was getting error remove Parent View . How to carry out the process?
case 3: new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Arihant Shopee Registeration")
.setMessage("Your Code doesn't match.Try Again")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() { #Override
public void onClick(DialogInterface dialog, int which) {
removeDialog(4);
showDialog(4);
}}).show();
break;
case 4:AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Enter Your Registeration Code");
alert.setMessage("Registeration code has been delivered on your registered number via sms");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString().trim();
String regsCode2=etFake.getText().toString().trim();
System.out.println("Va "+value+" Reg"+regsCode2);
if(value.compareToIgnoreCase(regsCode2)==0){
validCodeMatch=objCommonServices.sendEvalidCode(etMobileNo.getText().toString().trim(), etPassword.getText().toString().trim(),"OK");
if(validCodeMatch.contains("Code Match")){
showDialog(2);
}
}
else{
removeDialog(3);
showDialog(3);
}
}
});

Categories