繁体   English   中英

无法在AsyncTask中启动新活动

[英]Cannot start new activity in the AsyncTask

这是我的登录活动。 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); 

    }

}
}

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

您可以通过以下代码调用活动,

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM