1

I got this error in my codes when i use finish(); "The method finish() is undefined for the type SaveImageTask" . Am I missing some declarations or anything. Can someone please guide me on this.

    public class SaveImageTask extends AsyncTask<String , String , String> {

            private Context context;
            private ProgressDialog pDialog;

            boolean bCancelled=false;
            public SaveImageTask(Context context) {
                this.context = context;
            }

            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub

                super.onPreExecute();

                pDialog = new ProgressDialog(context);
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(true);
                pDialog.setOnCancelListener(cancelListener);
                pDialog.show();

            }

            @Override
            protected String doInBackground(String... args) {
                // TODO Auto-generated method stub

                // MY STUFF
                return null;   
            }

            @Override
            protected void onPostExecute(String args) {
                // TODO Auto-generated method stub

                pDialog.dismiss();

            }

            OnCancelListener cancelListener=new OnCancelListener(){
                @Override
                public void onCancel(DialogInterface arg0){
                    bCancelled=true;
                    SaveImageTask.this.finish(); // <<----"The method finish() is undefined for the type SaveImageTask"
                }
            };


        }
Android Novice
  • 536
  • 3
  • 14
  • 33

3 Answers3

3

use SaveImageTask.this.finish();

Shine
  • 3,788
  • 1
  • 36
  • 59
2
 OnCancelListener cancelListener=new OnCancelListener(){
                @Override
                public void onCancel(DialogInterface arg0){
                    bCancelled=true;
                    finish(); // <<----"The method finish() is undefined for the type new DialogInterface.OnCancelListener(){}"
                }
            };

you are creating an anonymous class here. Only final variables and methods of the outer class are accessible inside an anonymous class directly. Activity 's finish is not a final method. What you need to do is reference the 'outer' class, which is an Activity and call its finish()

ActivityName.this.finish()

Note : you cannot make a static call like ActivityName.finish() hence the reference of activity's object this.

Vinay W
  • 9,912
  • 8
  • 41
  • 47
2

Just use,

(Activity(context)).finish();

EDIT:

Actually you have to cast your context to Activity Context,

then you can use finish() method of Activity,

Something like,

private Activity context;

public SaveImageTask(Context context) {
                this.context = (Activity)context;
            }

now, just

context.finish();
user370305
  • 108,599
  • 23
  • 164
  • 151
  • I've already pass it on top. Got this error "The method Activity(Context) is undefined for the type new DialogInterface.OnCancelListener(){}" – Android Novice Aug 31 '12 at 15:10