我需要一些帮助来尝试解决这个问题,它以前可以工作,但是在更改了两个代码之后,可能是一个问题。也许是因为我从片段转换为活动。
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());
}
}
});
是的,所以它是在后台任务=新的后台任务的设置下,它显示了一个错误,指出“后台任务中的后台任务(上下文)无法应用于()。
这是我的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;
}
最佳答案
您的构造函数需要一个上下文。更改
BackgroundTask backgroundTask = new BackgroundTask();
至
BackgroundTask backgroundTask = new BackgroundTask(getActivity());