0

私のアプリケーションでは、関数(テキストビューの値を更新する)を呼び出す必要があります。OKボタンが押されたときにAlertDialg内でこの関数を呼び出す必要があります。

問題は、ダイアログの[OK]ボタンが押された後にRefreshData.execute()を呼び出す方法です。
これはエラーの1つです:android.view.WindowManager $ BadTokenException:ウィンドウを追加できません-トークンnullはアプリケーション用ではありません。

コード:

    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;

        }

    }

}

4

1 に答える 1

2

代わりに

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

で試してみてください

this.progressDialog = ProgressDialog.show(yourActivity.this, ""," Loading..."); 
于 2012-04-05T16:27:07.310 に答える