Bundle.getString()似乎没有获取值

时间:2016-11-13 18:34:00

标签: java android bundles

我希望我的MainActivity从两个不同的活动中得到额外的东西,我认为与两个活动区别的唯一方法是检查Bundles的关键,但是当我试图获得它们来自的附加内容时,它们都是空。

主要活动:

public class UserActivity extends AppCompatActivity {
public String token;
public String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user);
    Intent intent = getIntent();
    Bundle b = intent.getExtras();
    if(b.containsKey("createBundle"))
    {
        token = b.getString("sessionID");
        name = b.getString("nameID");
    }
    else if(b.containsKey("logInBundle"))
   {
       token = b.getString("loginToken");
       name = b.getString("loginName");
   }

    TextView welcome = (TextView) findViewById(R.id.welcomeView);
    welcome.setText("Hello, " + name);
} 

活动1:

public class CreateUserActivity extends Activity {
EditText txtName, txtEmail, txtPassword;
AlertDialogManager alert = new AlertDialogManager();
private String token;
private SharedPreferenceManager sharedPreferenceManager;

public void setToken(String token) {
    this.token = token;
}

public String getToken() {
    return token;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.create_user_activity);
    txtName = (EditText) findViewById(R.id.loginName);
    txtEmail = (EditText) findViewById(R.id.loginEmail);
    txtPassword = (EditText) findViewById(R.id.loginPassword);
    sharedPreferenceManager = new SharedPreferenceManager();

}

public void checkCreateUser(View view) throws JSONException {
    String name = txtName.getText().toString();
    String email = txtEmail.getText().toString();
    String password = txtPassword.getText().toString();
    String tokenAgain = "";

    CheckBox checkBox = (CheckBox) findViewById(R.id.login_remember_checkbox);
    if (name.trim().length() > 0 && password.trim().length() > 0 && email.trim().length() > 0) {
        JSONObject userObj = new JSONObject();
        userObj.put("name", name);
        userObj.put("email", email);
        userObj.put("password", password);
        String jsonDocument = userObj.toString();
        PostUserTask put = new PostUserTask();
        put.execute("http://api.evang.dk/v2/users", jsonDocument);

        tokenAgain  = getToken();

        if (checkBox.isChecked()) {
            sharedPreferenceManager.saveData(CreateUserActivity.this, "USERNAME", name);
            sharedPreferenceManager.saveData(CreateUserActivity.this, "EMAIL", email);
            sharedPreferenceManager.saveData(CreateUserActivity.this, "PASSWORD", password);
            sharedPreferenceManager.saveData(CreateUserActivity.this, "TOKEN", tokenAgain);
        } else {
            sharedPreferenceManager.removeData(CreateUserActivity.this, "USERNAME");
            sharedPreferenceManager.removeData(CreateUserActivity.this, "EMAIL");
            sharedPreferenceManager.removeData(CreateUserActivity.this, "PASSWORD");
            sharedPreferenceManager.removeData(CreateUserActivity.this, "TOKEN");
        }

    }
    else
    {
        alert.showAlertDialog(CreateUserActivity.this, "Login failed!", "Please enter name, username and password", false);
    }
    Intent i = new Intent(getBaseContext(), UserActivity.class);
    Bundle b = new Bundle();
    b.putString("sessionID", tokenAgain);
    b.putString("nameID", name);
    i.putExtra("createBundle", b);
    startActivity(i);
}

活动2:

public class LoginActivity extends AppCompatActivity {
EditText userNameTxt, passwordTxt, emailTxt;
SharedPreferenceManager sharedPreferenceManager;
String userName;
String token;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    Intent intent = getIntent();
    userNameTxt = (EditText) findViewById(R.id.userNameLogin);
    passwordTxt = (EditText) findViewById(R.id.passwordLogin);
    emailTxt = (EditText) findViewById(R.id.emailLogin);
    sharedPreferenceManager = new SharedPreferenceManager();
    userName = sharedPreferenceManager.getData(this, "USERNAME");
    String email = sharedPreferenceManager.getData(this, "EMAIL");
    String password = sharedPreferenceManager.getData(this, "PASSWORD");
    token = sharedPreferenceManager.getData(this, "TOKEN");
    if(userName != null && email != null && password != null && token != null)
    {
        userNameTxt.setText(userName);
        emailTxt.setText(email);
        passwordTxt.setText(password);
    }
}

public void onClickLog(View view) {
    if(token != null)
    {
        Intent i = new Intent(getBaseContext(), UserActivity.class);
        Bundle b = new Bundle();
        b.putString("loginToken", token);
        b.putString("loginName", userName);
        i.putExtra("logInBundle", b);
        startActivity(i);
    }
}

}

1 个答案:

答案 0 :(得分:1)

根据您发布的代码,以下

 if(b.containsKey("createBundle")) {
        token = b.getString("sessionID");
        name = b.getString("nameID");
  } else if(b.containsKey("logInBundle")) {
       token = b.getString("loginToken");
       name = b.getString("loginName");
  }

错了。 onClickLog您正在使用密钥Bundle添加新的logInBundle。您应该使用getIntent().hasExtra(String)查看什么。 E.g。

if(getIntent().hasExtra("createBundle")) { 
   b = getIntent().getBundleExtra("createBundle");
} else if (getIntent().hasExtra("logInBundle")) {
   b = getIntent().getBundleExtra("logInBundle");
}

然后从你正在做的b读取