Android编程,后台任务中的后台任务(Context)无法应用于()

时间:2016-04-29 15:15:35

标签: java android background-task

我需要一些帮助来试图解决这个问题,它之前有效,但在更改了几个代码之后,它可能是一个问题。也许是因为我从片段转变为活动。

btnRegister.setOnClickListener(new android.view.View.OnClickListener() {
            @Override
            public void onClick(android.view.View v) {

                if (name.getText().toString().equals("") || email.getText().toString().equals("") || pw.getText().toString().equals("")) {


                    android.app.AlertDialog.Builder alertDialogBuilder =
                            new android.app.AlertDialog.Builder(Register.this);
                    alertDialogBuilder.setTitle("Something went wrong");
                    alertDialogBuilder.setMessage("Please check and fill in all the fields");
                    alertDialogBuilder.setPositiveButton("Ok", new android.content.DialogInterface.OnClickListener() {
                        public void onClick(android.content.DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    //displaying of alert dialog
                    android.app.AlertDialog alertDialog = alertDialogBuilder.create();
                    alertDialog.show();
                }
                //checking pw and confirm pw
                else if (!pw.getText().toString().equals(pwConf.getText().toString())) {

                    android.app.AlertDialog.Builder alertDialogBuilder =
                            new android.app.AlertDialog.Builder(Register.this);
                    alertDialogBuilder.setTitle("Something went wrong");
                    alertDialogBuilder.setMessage("Your passwords are not matching");
                    alertDialogBuilder.setPositiveButton("Ok", new android.content.DialogInterface.OnClickListener() {
                        public void onClick(android.content.DialogInterface dialog, int which) {
                            dialog.dismiss();
                            pw.setText("");
                            pwConf.setText("");
                        }
                    });
                    android.app.AlertDialog alertDialog = alertDialogBuilder.create();
                    alertDialog.show();
                }

                else{
                    BackgroundTask backgroundTask = new BackgroundTask();
                    backgroundTask.execute("register", name.getText().toString(), email.getText().toString(), pw.getText().toString());
                }
            }
        });

是的,所以它是在Background task = new Background任务的设置下,它显示一个错误,指出“后台任务中的后台任务(上下文)不能应用于()。

这是我的backgroundtask类。

public class BackgroundTask extends android.os.AsyncTask<String, Void, String>{

    android.content.Context ctx;
    android.app.Activity activity;
    android.app.ProgressDialog progressDialog;
    String register_url = "http://10.0.2.2/mydb/register.php";
    android.app.AlertDialog.Builder builder;


    public BackgroundTask(android.content.Context ctx) {

        this.ctx = ctx;
        activity = (android.app.Activity)ctx;

    }

1 个答案:

答案 0 :(得分:2)

您的构造函数需要Context。变化

BackgroundTask backgroundTask = new BackgroundTask();

BackgroundTask backgroundTask = new BackgroundTask(getActivity());