1

当我使用finish();"The method finish() is undefined for the type SaveImageTask" 时,我的代码中出现此错误。我是否遗漏了一些声明或任何东西。有人可以指导我吗?

    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"
                }
            };


        }
4

3 回答 3

3

利用SaveImageTask.this.finish();

于 2012-08-31T14:56:21.720 回答
2

只需使用,

(Activity(context)).finish();

编辑:

实际上,您必须将上下文转换为活动上下文,

然后你可以使用finish()Activity的方法,

就像是,

private Activity context;

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

现在,只是

context.finish();
于 2012-08-31T15:08:06.840 回答
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(){}"
                }
            };

你在这里创建一个anonymous class。只有外部类的最终变量和方法才能在匿名类中直接访问。Activity'sfinish不是最终方法。您需要做的是引用“外部”类,它是一个Activity并调用它finish()

ActivityName.this.finish()

注意:您不能像ActivityName.finish()引用活动对象那样进行静态调用this

于 2012-08-31T15:02:14.670 回答