I am Having Nested Tab Activity.
TabMain (TabHost) with that i have ChildTab (another TabHost) and other 2 Activities.
I can able to show Progress Dialog in that other 2 activity.
But with in ChildTab TabActivity I add 5 Activities
in that i can able to Show progressDialog.
I am getting the following error.
ERROR/AndroidRuntime(339): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@43d304f0 is not valid; is your activity running?
Try ProgressDialog progressDialog = ProgressDialog.show(getParent(), "Loading...", "Please wait...");
...I think it will make things worinkg .
Ok. I found this error too and became mad till I found the solution.
The answer above is correct but in my case I had a nested application formed by tabs/activity groups in/more activity groups and a single activity inside. You need to go up as many levels as you need till you get the context of the application. In my case there were two:
public static Activity goUp(Activity current){
if(current.getParent()!=null){
current=current.getParent();
goUp(current);
}
return current;
}
ProgressDialog progressDialog = ProgressDialog.show(goUp(MyActivity.this), "Loading...", "Please wait...");
Ugly solution :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
parent = (ConfigurarionStyleAndroidGUI) getParent();
addPreferencesFromResource(R.xml.ui_settings);
EditTextPreference p = (EditTextPreference) getPreferenceManager().findPreference(getString(R.string.key_settings_style_name));
forceContext(parent,p);
}
private void forceContext(Context context,Preference p){
try {
Field field = Preference.class.getDeclaredField("mContext");
field.setAccessible(true);
field.set(p, parent);
} catch (Exception e) {
e.printStackTrace();
}
}
If getParent() doesn't work for you, try using just TabsActivity.context
(or substitute the name of your parent tab activity class). I am using nested activities and as a result using getParent() is still not returning the right context for the dialog.
After trying 20 different variations of the suggestions above I replaced this line:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
With:
AlertDialog.Builder builder = new AlertDialog.Builder(TabsActivity.context);
and it worked like a charm. You'll also need to create the context variable in the TabsActivity class. Something like public static TabsActivity context;
and context=this
in the onCreate method.