I have this method in which i want to create an AlertDialog but when I call it the application stops working when it reaches ad.show
public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS is necessary for Traffic Finder");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
//mContext.startActivity(intent);
startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// Create Alert Dialog
AlertDialog ad= alertDialog.create();
// Showing Alert Message
ad.show();
}
I get this:
08-26 20:47:11.462 28940-29005/? E/AndroidRuntime﹕ FATAL EXCEPTION: IntentService[CoordinatesService]
Process: com.example.manos.trafficfinder, PID: 28940
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:566)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:282)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.app.Dialog.show(Dialog.java:298)
at com.example.manos.trafficfinder.GPSTracker.showSettingsAlert(GPSTracker.java:182)
at com.example.manos.trafficfinder.GeographicCoordinates.getLatitudeString(GeographicCoordinates.java:32)
at com.example.manos.trafficfinder.DataManagement.addPosition(DataManagement.java:116)
at com.example.manos.trafficfinder.CoordinatesService.onHandleIntent(CoordinatesService.java:28)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)
The Context passed to AlertDialog.Builder seems incorrect. Try YourClass.this instead of mContext.
Related
I have a "alertDialogBuilder" to rename an entry when a button is pressed. This works fine when the App is freshly opened. But If I press the back button (meaning the App is minimized and I am back at the Android home-screen), when I relaunch the App and press the button, this time the APP crashes. This happens every time and I have no idea how to debug this. I have checked the lifecycle and "onPause" and "onStop" are called when pressing the back button. But I don't see why that should be a problem.
Any ideas?
Here is the code where I launch the prompt dialogue in a helper class:
public void loadPromptInput(Context promptcontext, final OnOkGetText onOk, String InitialTxt) {
//pathText.setText("Prompt input");
LayoutInflater li = LayoutInflater.from(promptcontext);
View promptsView = li.inflate(R.layout.prompts_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( promptcontext);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editTextDialogUserInput);
userInput.setText("");
userInput.append(InitialTxt);
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
onOk.hereIsYouText(userInput.getText().toString());
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
// make the keyboard shown by default
alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com...., PID: 31622
android.view.WindowManager$BadTokenException: Unable to add window --
token android.os.BinderProxy#423c9940 is not valid; is your activity
running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:532)
at
android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:286)
at
com....myUtils.loadPromptInput(myUtils.java:71)
at
com....MainActivity$6.onReceive(MainActivity.java:557)
at
android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:308)
at
android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:46)
at
android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:118)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5095)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at
com.zte.heartyservice.SCC.FrameworkBridge.main(FrameworkBridge.java:136)
at dalvik.system.NativeStart.main(Native Meth
od)
It would be helpful looking at your code to see where you call:
void loadPromptInput(Context promptcontext...
...most probably you are passing as parameter an instance of a context no more valid.
In any case before calling your method check if the activity is finishing:
//in a fragment
if(getActivity() != null && !getActivity().isFinishing()) {
loadPromptInput(getActivity()...
}
//in an activity
if(!isFinishing()) {
loadPromptInput(this...
}
I am asking the same question which is asked before at below links but the solution proposed in these links is not working for me so I am posting it again.
How to make an AlertDialog disappear?
Android AlertDialog always exits when I click on OK Button
How to navigate to next activity after user clicks on AlertDialog OK button?
Basically, I am creating an AlertDialog builder to notify the user for asking to enable a setting for the Usage Data Access and when the OK button is pressed then the Settings menu gets opened. When I press back button to come back on the app then the AlertDialog is still available there although I expected to be dismissed to be back on my app.
public void show_alert(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("This application requires access to the Usage Stats Service. Please " +
"ensure that this is enabled in settings, then press the back button to continue ");
builder.setCancelable(true);
builder.setPositiveButton(
"OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivity(intent);
dialog.dismiss();
}
});
builder.show();
return;
}
Any hint what wrong could be going here?
Edit after some testing:
I tested OPs code on 6.0.1 and it behaved as expected - i.e. the dialog was dismissed after clicked 'OK'. I'll leave my initial answer below as an alternative that also works. Additional alternatives can be found here.
You can get a reference to your Alert Dialog it from your builder.show() method:
mMyDialog = builder.show();
In your onClick method:
mMyDialog.dismiss();
Full sample:
AlertDialog mMyDialog; // declare AlertDialog
public void show_alert(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("This application requires access to the Usage Stats Service. Please " +
"ensure that this is enabled in settings, then press the back button to continue ");
builder.setCancelable(true);
builder.setPositiveButton(
"OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivity(intent);
mMyDialog.dismiss(); // dismiss AlertDialog
}
});
mMyDialog = builder.show(); // assign AlertDialog
return;
}
In my code, I displayed an alert dialog when the internet is not available. In that dialog I set a message "Internet Not Available." In that dialog I put two buttons 'Refresh' & 'Setting'. When the internet's gone & dialog comes, I clicked on Setting button it gives
This is the error (Logcat).
E: FATAL EXCEPTION: main Process:
com.appiphy.spacecomp.spinnerframework1, PID: 30424
java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread
android.app.ActivityThread.getApplicationThread()' on a null object
reference
at android.app.Activity.startActivityForResult(Activity.java:3823)
at android.app.Activity.startActivityForResult(Activity.java:3784)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:784)
at android.app.Activity.startActivity(Activity.java:4094)
at android.app.Activity.startActivity(Activity.java:4062)
at com.appiphy.spacecomp.spinnerframework1.activity.MainActivity$10.onClick(MainActivity.java:967)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5297)
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:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
I used above line to open Settings on a device.
How to solve the above error?
Try this:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_Holo_Light_Dialog));
alertDialogBuilder
.setMessage("No internet connec" +
"" +
"tion on your device. Would you like to enable it?")
.setTitle("No Internet Connection")
.setCancelable(false)
.setPositiveButton(" Enable Internet ",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent dialogIntent = new Intent(android.provider.Settings.ACTION_SETTINGS);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
}
});
alertDialogBuilder.setNegativeButton(" Cancel ", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
I don't know whether your dialog is inside your activity or in some other utility class. If its outside the activity, then you should use context to start the intent. You can pass the context of the activity, and call
Intent intent = new Intent(android.provider.Settings.ACTION_SETTINGS));
mContext.startActivity(intent); mContext is the context which you will pass fro the activity.
ongoing video call got disconnected when it goes to on pause because of do not keep activities checked under developer option in android device how can a user stay in video instead of that option is checked
You can check "Is the do not keep activities is checked or not" by using below line,
int value = Settings.System.getInt(getContentResolver(), Settings.System.ALWAYS_FINISH_ACTIVITIES, 0);
If it returns one, create a AlertDialog and ask the user to disable it first, if user clicks on positive button then create an intent for the Settings like below,
int value = Settings.System.getInt(getContentResolver(), Settings.System.ALWAYS_FINISH_ACTIVITIES, 0);
if (value == 1)
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("");
// set dialog message
alertDialogBuilder
.setMessage("You need to disable `do not keep activities` option, press YES")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.dismiss();
startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS));
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
I am entirely not sure if I got you correctly.
But does this: https://developer.android.com/training/scheduling/wakelock.html
answer your question?
It prevents your app from entering onPause, when a voice-chat is ongoing.
I am currently working on an android project. I am trying to have an alert dialogue in a standard java class so that the code can be re-used throughout the app.
However, it is returning the alertdialog from the class back to the activity but when I attempt to show the alert dialog it displays the following error:
Unable to add window -- token null is not for an application
Below is the code that I have used to create the alert dialogue
public AlertDialog showAlertDialog(String message, Context context)
{
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("hello")
.setCancelable(false)
.setPositiveButton("Yes", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
return alert;
}
Below is the code from the android activity where I am trying to show the alert dialog
Common cla = new Common();
AlertDialog alert = cla.showAlertDialog("Hello", getApplicationContext());
alert.show();
Common is the name of the class
Please chage your AlerDialogCreation logic to AlertDialog.Builder builder = new AlertDialog.Builder(yourActivity.this);