gpt4 book ai didi

java - 无法在 AsyncTask 中启动新 Activity

转载 作者:行者123 更新时间:2023-12-01 10:30:56 27 4
gpt4 key购买 nike

这是我的登录 Activity 。 loginButtonListener 声明一个 AsyncTask 变量来处理远程 MySQL 上的验证信息

public class LoginActivity extends AppCompatActivity implements AsyncResponse {
private EditText editText_email;
private EditText editText_password;
private Button button_login;
private Button button_register;

@Override
protected void onCreate(Bundle savedInstanceState) {
BackgroundWorker worker = new BackgroundWorker(LoginActivity.this);
loginButtonListener();
registerButtonListener();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}



public void loginButtonListener(){
button_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String type = "login";
String email = editText_email.getText().toString();
String password = editText_password.getText().toString();

BackgroundWorker worker = new BackgroundWorker(LoginActivity.this);
worker.execute(type, email, password);

}
});

}

}

这是我的backgroundwork.class

public class BackgroundWorker extends AsyncTask<String,Void,String> {
Context context;
AlertDialog alertDialog;
public AsyncResponse delegate = null;
BackgroundWorker (Context ctx) {
context = ctx;
}

// Http MySQL stuffs


@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login Status");
}

@Override
protected void onPostExecute(String result) {
//LoginActivity.get=result;
//delegate.processFinish(result);
alertDialog.setMessage(result);
alertDialog.show();
if(result.contains("Remote login success")){
Intent i = new Intent(LoginActivity.this, MainpanelActivity.class); // ERROR HERE, FIRST ARGUMENT IS NOT AN ENCLOSING CLASS
i.putExtra("email", this.email); /
startActivity(i);

}

}
}

但是,当我尝试启动新 Activity (登录成功,跳转到另一个 Activity )时,我在 onpostexecute() 中收到“xxx 不是封闭类错误”

最佳答案

您可以从以下代码调用该 Activity ,

if(result.contains("Remote login success")){
Intent i = new Intent(context, MainpanelActivity.class); //use context here
i.putExtra("email", this.email);
startActivity(i);

}

关于java - 无法在 AsyncTask 中启动新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35097882/

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