gpt4 book ai didi

java - null return Intent on android编程

转载 作者:行者123 更新时间:2023-11-29 08:39:16 24 4
gpt4 key购买 nike

我在 android studio 中编写了一个简单的程序,并从 Intent 创建了一个对象。但我不知道为什么它返回 null ??

在 MainActivity.java 中,我将用户名和密码发送到 ProfileActivity.java。但是 Log.i 显示为空!但 token 不为空

com.example.sayres.myapplication2.ProfileActivity: onCreate User Name: null Password: null Token: 22546874569

主要 Activity .java:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public static final String INTENT_USER_NAME_KYE = "user_name_key";
public static final String INTENT_USER_PASSWORD_KYE = "password_key";

protected final String TAG = "====>";

private EditText editTextUserName, editTextPassword;
private Button buttonLogin, buttonExit;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();


}


@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnLogin:
getUserInfo();
break;
case R.id.btnExit:
finish();
break;
}


}



private void initView() {
editTextUserName = (EditText) findViewById(R.id.editUserName);
editTextPassword = (EditText) findViewById(R.id.editPassword);
buttonExit = (Button) findViewById(R.id.btnExit);
buttonLogin = (Button) findViewById(R.id.btnLogin);

buttonLogin.setOnClickListener(this);
buttonExit.setOnClickListener(this);
}


private void getUserInfo() {
String userName = editTextUserName.getText().toString();
String password = editTextPassword.getText().toString();
editTextUserName.setText("");
editTextPassword.setText("");


String msg = "";
if (userName.isEmpty() || password.isEmpty()) {
msg = "Insert UserName And Password !!!";
} else {
msg = "Welcome " + userName;
// TODO: 1/7/2017 go to ProfileActivity !!!

Intent intent = new Intent(this, ProfileActivity.class);
intent.putExtra("INTENT_USER_NAME_KYE", userName);
intent.putExtra("INTENT_USER_PASSWORD_KYE", password);

Bundle bundle = new Bundle();
bundle.putString("user_token", "22546874569");
intent.putExtras(bundle);

startActivity(intent);
finish();
}

Log.i(TAG, "UserName= " + userName + "\t" + "Password= " + password);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
}

}

ProfileActivity.java:

public class ProfileActivity extends AppCompatActivity {
private static final String TAG = ProfileActivity.class.getName();
private TextView textViewUserName, textViewPassword, textViewAge, textViewSex;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
// initviews();
Intent intent = getIntent();
String userName = intent.getStringExtra(MainActivity.INTENT_USER_NAME_KYE);
String password = intent.getStringExtra(MainActivity.INTENT_USER_PASSWORD_KYE);

// System.out.println("User Name iS:"+userName);
Bundle extras = intent.getExtras();
String user_token = extras.getString("user_token");

Log.i(TAG, "onCreate " + "User Name: " + userName + " Password: " + password + " Token: " + user_token);
}

最佳答案

intent.putExtra("INTENT_USER_NAME_KYE", userName);
intent.putExtra("INTENT_USER_PASSWORD_KYE", password);

您使用的是文字 "INTENT_USER_NAME_KYE" 作为 intent 键,而不是用于阅读附加内容的 INTENT_USER_NAME_KYE 字段的值:

String userName = intent.getStringExtra(MainActivity.INTENT_USER_NAME_KYE);
String password = intent.getStringExtra(MainActivity.INTENT_USER_PASSWORD_KYE);

删除 "

关于java - null return Intent on android编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41524629/

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