簡體   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