I'm trying to display an AlertDialog inside of another AlertDialog. When the user clicks on the any item within the initial AlertDialog another AlertDialog is created and shown.
I'm following the correct pattern for creating and displaying AlertDialogs, the problem is as soon as the code reaches the point where the innerDialog.show() method is encountered the application fails. The logcat prints an uncaught runtime exception:
android.view.WindowManager$BadTokenException : Unable to add window -- token null is not for an applicaion
I'm wondering if i'm allowed to call the show() method on the innerAlertDialog manually.
The outer AlertDialog is working because i'm using the callback onCreateDialog() method.
In previous versions there was a bug where getApplicationContext() returned null.
I still dont quite if it has been fixed however with dialogs, its always better to send this.
And for the main question, its working as designed to avoid locking the UI thread.
I have seen people recommending creating a new layout with the theme of the dialog and start the activity on the first dialog, however I havent try this yet.
Related
Some people are getting this even tho I check if the activity is finishing or not. This app has a few million users and only 10 or so people are crashing. I am confused as to why.
private fun someFun(headerTitle: String, message: String) {
if (isFinishing) return
ComponentPopup.Builder(this)
.setTitleText(headerTitle)
.setMessageText(message)
.setPositiveButtonText("Some text")
.setNegativeButtonText("Some text")
.createPopup()
.show()
}
ComponentPopup is just a custom component that extends Dialog.
Crashlytics says I get this exception at .show(). I don't call this function on a background thread either. Only on onBackPressed().
What gives? Why do I get this exception even though I check isFinishing?
Tried to finish the activity manually. Still wasn't able to produce the problem.
I was getting the same exception. In my case the issue was I was passing the application context to the Builder instead of the Activity context. Hopefully, you are not doing so.
Just for testing, you may pass the context of your current Activity as YOUR_ACTIVITY.this instead of this.
This Issue happens when the user leave the activity before the diloag shown. So the dilaog didn't get the window of the specified Actvity to be show on.
You can generate the crash by calling the show of dialog and then instantly leave the activity.
I am getting an error as Null pointer exception and unable to instantiate activity when I am creating a toast in activity constructor.I want to know the reason why toasts are working in onCreate method but not in activity constructor
The OS is responsible for constructing your Activity classes. This is because it needs to perform some setup, including providing the Activity a proper base Context. Without this, you cannot make Toasts.
In general, you should avoid doing anything in the constructor of an Activity (and you should definitely not be making instances of them yourself using new).
Usually a Toast appears as a feedback to user input (say a button click) or when some external event is registerd (say a network error happend or new incoming data is available).
However, to me it sounds as though you want to show a Toast right after opening your activity, is that correct? Then you should put it in the onCreate method of your activity. An example Toast is made like this
Toast.makeText(MainActivity.this, "Hello World", Toast.LENGTH_LONG).show();
You might also consider putting this call in the onResume method. I recommend getting familiar with activity lifecycles.
Instead of using Toasts you might also want to check out Snackbars.
I'm new to Android and I'm programing an application with multiple user interfaces(windows).
I find it a little hard to define new activity for each window so I end up with using Dialogs more than activity. But there is a scenario where I need to sequentially show multiple dialogs.
In c# showing Dialog is blocking operation. But I don't know how to do that in Android.
if there, I welcome any alternatives.
If you want to show a sequence of dialog, you can use the onclick listeners. From one dialog, open the following one. ( dialog interface for the listeners)
and if you want to block your program, so the user has to click on the dialog, set the dialogs not cancelable (setCancelable)
OK with no code reference I would say the easiest way would be using ondimiss listeners in each dialog for the next one to be called. You can check out this short example to get an idea of implementation (note they are using ondismiss for something else).
http://android-er.blogspot.com/2011/11/cancel-progressdialog.html
While running monkey tool on my app, I am getting android.view.WindowLeaked exception, I referred some stackoverflow threads and found that we need avoid this while creating popups, instead we need to use getApplicationContext() but this is causing BadTokenException
E/AndroidRuntime(5597): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
Again after searching through I found this answer which says
One cause of this error may be trying to display an application
window/dialog through a Context that is not an Activity
So how to solve this?
Usually window leaks because You don't use showDialog method in Activity but only using Dialog.show() method.
When dialog is displayed and configuration of Your Activity changes (eg. orientation change) dialog has not window to attach, and this results in leak of window.
If You use Compatibility library You should use DialogFragment instead of pure dialog.
the reason behind WindowManager$BadTokenException is that you keep showing dialog or popupwindow on the window whose context is not alive now.
So should always dismiss popup or dialog whenever you switch between activities or dismiss any activity.
Therefore you should probably call dilog.dismiss() in onPause method.
onPause()
{
dilog.dismiss()
}
I have an activity with in which there is a async task that will do some download stuff. AT the time of downlaoding it will show a loading dialog.
My problem is, it worked fine for me when me doing it in only one orentiaon. But when i rotate at the time of download, it shows window leaked and will crash at the
dialog.cancel in my post excute.
From my study on it more i understood it due the change in the context when device is rotated.
That is when a device is rotated the activity will be recreated so the context will be changed.
But i have created the dialog with old one and that wasn't the current context. So when i cancel it it shows error
What is the solution for this, any idea frnds.
Me using honeycomb, me tried but with fragment but didnt get a good sample for that. Me now mainly trying that,
if anyone can give me some links for that it will be
great
First of all: open your dialog using the showDialog method (there are a lot of examples in the official documentation). If you do so, the activity will take care of dismissing the dialog on destroy, and re-showing it after the activity has been recreated.
Also... if the dialog shows a progress bar (not a wheel), you will want to update the progress of the dialog after orientation changes. In order to do so, I recommend to use the onRetainNonConfigurationInstance to return the current state of the dialog and/or the activity itself. Then, you can use getLastNonConfigurationInstance to recover that state. Google about those two methods if you want to see examples.
Another thing to keep in mind: if you are updating the state of the dialog an/or any other UI element from the AsyncTask, you must be aware that after the activity is recreated, the AsyncTask may be pointing to the wrong UI references. In order to handle this, you can create a proxy class (Proxy design pattern) to detach the AsyncTask progress notifications from the current UI elements.