简体   繁体   中英

Custom dialog in android asynctask

Whats the wrong in onPostExecute. Its showing the error in that method. Working fine for ProgressDialog. json and database handlers are all working properly. Same thinf has been done for ProgressDialog.

class ProcessBalance  extends AsyncTask<String, Void, List<String>> {

        private Dialog dialog;

        /**
         * Before starting background thread
         * Show Progress Bar Dialog
         * */
        @Override
        protected void onPreExecute() {

             final Dialog dialog = new Dialog(UserhomeActivity.this);
             dialog.setTitle("test");
             dialog.setContentView(R.layout.dialoglayout);
             dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
             Button btnDismiss = (Button)dialog.getWindow().findViewById(R.id.dismiss);
           //  TextView balview = (TextView) dialog.findViewById(R.id.textView1);
        //     balview.setText("Please wait for the balance.");

             //http://www.mkyong.com/android/android-custom-dialog-example/

             btnDismiss.setOnClickListener(new OnClickListener(){

               public void onClick(View v) {
                   dialog.dismiss();
               }});

             dialog.show();
        }

        /**
         * Downloading file in background thread
         * */
        @Override
        protected List<String> doInBackground(String... args0) {
        //  DatabaseHandler db = new DatabaseHandler(getApplicationContext());
            //HashMap<String, String> user = db.getUserDetails();
            UserFunctions userFunction = new UserFunctions();
            //JSONObject json = userFunction.getBalance(user.get("mobile_no"));
            JSONObject json = userFunction.getBalance("1234567890");
            List<String> LIST = new ArrayList<String>();
            try {
                if (json.getString(KEY_SUCCESS) != null) {
                    String res = json.getString(KEY_SUCCESS);
                    LIST.add(json.getString("success"));
                    LIST.add(json.getString("balance"));
            }

            } catch (NullPointerException e) {
                e.printStackTrace();

            }
            catch (JSONException e) {
                e.printStackTrace();
            }

            return LIST;
        }


        /**
         * After completing background task
         * Dismiss the progress dialog
         * **/

        protected void onPostExecute(List<String> result) {

            String responseCodestr = result.get(0);
            final int responseCode = Integer.parseInt(responseCodestr);
            String bal = result.get(1);
            TextView balview = (TextView) dialog.findViewById(R.id.textView1);

            if(responseCode==1){
                 balview.setText("test "+bal);
            } else {
                  balview.setText("test2");
            }

        }

      }

You have two dialogs, one as a member an another final one defined in onPreExecute. You should change the below code

    private Dialog dialog;
    @Override
    protected void onPreExecute() {

         final Dialog dialog = new Dialog(UserhomeActivity.this);

for this:

    private final Dialog dialog;
    @Override
    protected void onPreExecute() {

         this.dialog = new Dialog(UserhomeActivity.this);

The issue is you have declared dialog twice.

One is before onPreExcecute

 private Dialog dialog;

One is inside onPreExcecute

final Dialog dialog = new Dialog(UserhomeActivity.this);

The dialog you are initializing is second one and the dialog you are trying to use in onPostExceute is first one.

Solution is

change

final Dialog dialog = new Dialog(UserhomeActivity.this);

to

dialog = new Dialog(UserhomeActivity.this);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM