gpt4 book ai didi

java - Android编程,后台任务(Context)中的后台任务不能应用到()

转载 作者:行者123 更新时间:2023-12-02 03:40:00 26 4
gpt4 key购买 nike

我需要一些帮助来解决这个问题,它以前有效,但在更改了几个代码后,这可能是一个问题。也许是因为我从 fragment 切换到 Activity 。

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());
}
}
});

是的,因此在后台任务=新后台任务的设置中,它显示错误,指出“后台任务中的后台任务(上下文)无法应用于()。

这是我的后台任务类。

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());

关于java - Android编程,后台任务(Context)中的后台任务不能应用到(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36941964/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com