((对话问题))android.view.WindowManager $ BadTokenException: [英] ((Dialog issue ))android.view.WindowManager$BadTokenException:

查看:192
本文介绍了((对话问题))android.view.WindowManager $ BadTokenException:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序需要调用一个函数(更新values​​of textviews),我需要调用里面AlertDialg此功能时,确定按钮pressed。

In my Application I need to call a function(Which update the valuesof textviews),I need to call this function inside AlertDialg when OK button is pressed.

问题是我怎么能对话框的确定​​按钮后调用RefreshData.execute()是pressed?结果
这是错误之一:
 android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null不是一个应用程序

the issue is how i can call RefreshData.execute() after ok button of dialog is pressed ?
this is one of the Errors : android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application.

在code:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.details);

        Bundle extras = getIntent().getExtras();

        if (extras != null) {
            x = extras.getString("key").toString();
        } else {

            Toast.makeText(getBaseContext(), "null", 0).show();
        }

        tv_summary = (TextView) findViewById(R.id.tv_summary);
        tv_servings_result = (TextView) findViewById(R.id.tv_servings_result);
        tv_calories_result = (TextView) findViewById(R.id.tv_calories_result);
        tv_fat = (TextView) findViewById(R.id.tv_fat);
        tv_monofat = (TextView) findViewById(R.id.tv_monofat);
        tv_satfat = (TextView) findViewById(R.id.tv_satfat);
        tv_ch = (TextView) findViewById(R.id.tv_ch);
        tv_sug = (TextView) findViewById(R.id.tv_sug);

        new LoadDetails().execute();

        Button MealSize = (Button) findViewById(R.id.btn_size);

        MealSize.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                show();

                Toast.makeText(getBaseContext(), F + "", 0).show();
            }

        });

    }

    void Refresh() {
        new RefreshData().execute();
    }

    void show() {

        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("Title");
        alert.setMessage("Message");

        final EditText input = new EditText(this);
        alert.setView(input);
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

                F = Float.parseFloat(input.getText().toString());
                new RefreshData().execute();

            }
        });

        alert.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // Canceled.
                    }
                });

        alert.show();

    }

    private class RefreshData extends AsyncTask<Void, Void, Void> {

        private ProgressDialog progressDialog;

        @Override
        protected void onPreExecute() {
            this.progressDialog = ProgressDialog.show(getBaseContext(), "",
                    " Loading...");
        }

        @Override
        protected void onPostExecute(final Void unused) {

            this.progressDialog.dismiss();

            try {
                this.progressDialog.dismiss();
                tv_servings_result.setText(servings_result + "" + F);
                tv_calories_result.setText(cal + "g");
                tv_fat.setText(ff + "");
                tv_monofat.setText(mm + "");
                tv_satfat.setText(sasa + "");
                tv_ch.setText(chch + "");
                tv_sug.setText(sugar + "");

            } catch (Exception e) {
                Log.e("log_tag",
                        "Eraaaaaaaaaaaaaaaaaaa connection" + e.toString());

            }

        }

        @Override
        protected Void doInBackground(Void... params) {

            try {
                sugar = Float.valueOf(sug).floatValue();
                sugar *= F;

                cal = Float.valueOf(calories_result).floatValue();
                cal *= F;

                ff = Float.valueOf(fat).floatValue();
                ff *= F;

                mm = Float.valueOf(monofat).floatValue();
                mm *= F;

            } catch (Exception e) {
                Log.e("log_tag",
                        "Eraaaaaaaaaaaaaaaaaaa connection" + e.toString());

            }

            return null;

        }

    }

}

推荐答案

而不是使用

this.progressDialog = ProgressDialog.show(getBaseContext(), ""," Loading...");

尝试

this.progressDialog = ProgressDialog.show(yourActivity.this, ""," Loading..."); 

这篇关于((对话问题))android.view.WindowManager $ BadTokenException:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆