0

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.

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.

the 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;

        }

    }

}

1 Answer 1

2

instead using

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

try with

this.progressDialog = ProgressDialog.show(yourActivity.this, ""," Loading..."); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.