Issue Displaying ProgressDialog in Sub tab

I have a sub tab tab.

TabMain (TabHost) so that I have ChildTab (another TabHost) and the other 2 actions.

I can show the Progress Dialog in this other action.

But if in ChildTab TabActivity add 5 actions

in that I can show ShowDialog.

I get the following error.

ERROR / AndroidRuntime (339): caused by: android.view.WindowManager $ BadTokenException: cannot add window token android.app.LocalActivityManager$LocalActivityRecord@43d304f0 is invalid; Does your activity work?

+3
source share
4 answers

Try ProgressDialog progressDialog = ProgressDialog.show(getParent(), "Loading...", "Please wait...");

... I think it will make everyone worry.

+8

Ok. , . , , / / . , , . :

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...");
+2

:

        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();
        }           
    }
+2

getParent() , TabsActivity.context ( ). , getParent() - .

20 :

AlertDialog.Builder builder = new AlertDialog.Builder(this); 

AlertDialog.Builder builder = new AlertDialog.Builder(TabsActivity.context); 

and it worked like a charm. You will also need to create a context variable in the TabsActivity class. Something like public static TabsActivity context;, and context=thisin the method onCreate.

+1
source

Source: https://habr.com/ru/post/1789546/


All Articles