我有两个独立的文件, Login.java 和 LoginTask.java
我的问题是我试图将布尔值" checkFlag" 传递给 onPostExecute方法。
但布尔值总是 null 。代码如下:
Login.java
public class Login extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{
EditText etUsername, etPassword;
String login_name, login_pass;
CheckBox cbRemember;
public static final String ID="ID";
Boolean checkFlag;
SharedPreferences pref;
SharedPreferences.Editor editor;
final String TAG = this.getClass().getName();
@Override
protected void onCreate(Bundle savedInstanceState) {
etUsername =(EditText)findViewById(R.id.etUsername);
etPassword =(EditText)findViewById(R.id.etPassword);
cbRemember = (CheckBox)findViewById(R.id.cbRemember);
cbRemember.setOnCheckedChangeListener(this);
checkFlag = cbRemember.isChecked();
Log.d(TAG, "Check flag is : " + checkFlag);
pref = getSharedPreferences("login.conf", Context.MODE_PRIVATE);
editor = pref.edit();
}
public void userLogin (View view){
login_name = etUsername.getText().toString();
login_pass = etPassword.getText().toString();
checkFlag = cbRemember.isChecked();
String login_url = getString(R.string.login);
String method = "login";
LoginTask loginTask = new LoginTask(this);
loginTask.execute(method,login_url,login_name, login_pass, String.valueOf(checkFlag));
Toast.makeText(getApplicationContext(), "Check flag value = " + checkFlag, Toast.LENGTH_SHORT).show();
finish();
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkFlag = isChecked;
Log.d(TAG, "Check flag is : " + checkFlag);
}
}
LoginTask.java
public class LoginTask extends AsyncTask<String,Void,String>{
public static final String NAME="NAME";
public static final String ID="ID";
private PrefManager pref;
Context ctx;
LoginTask(Context ctx){this.ctx=ctx;}
@Override
protected void onPreExecute(){super.onPreExecute();}
@Override
protected String doInBackground(String... params){}
@Override
protected void onProgressUpdate (Void... values){super.onProgressUpdate(values);}
// I want to pass checkFlag boolean value into this method
@Override
protected void onPostExecute (String result){
String fail = "Try Again";
String login_name = result.trim(); // display "try again" error
String welcome = "Welcome to E-Movement";
pref = new PrefManager(ctx);
if(result.trim().equalsIgnoreCase(fail.trim())){
pref.logout();
Toast.makeText(ctx,"Login failed ! " + fail, Toast.LENGTH_LONG).show();
Intent intent = new Intent(ctx, Login.class);
ctx.startActivity(intent);
}else{
Toast.makeText(ctx, "Check flag is = " + checkFlag, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(ctx, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(intent);
pref.createLoginSession(login_name);
}}}
答案 0 :(得分:0)
new LoginTask(this);
将所有需要的参数作为参数放在构造函数中。正如您对上下文所做的那样。
new LoginTask(this, checkFlag);
其他参数也是如此。你现在怎么做很乱。