gpt4 book ai didi

java - 字符串无法在第二个 Activity 中显示

转载 作者:行者123 更新时间:2023-11-29 04:53:15 24 4
gpt4 key购买 nike

我试图在第二个 Activity 中显示用户输入和密码,但我收到一个错误,不幸的是登录已停止...我收到错误 java.lang.nullpointerException:尝试在空对象引用上调用虚拟方法'void android.widget.TextView.setText(java.lang.ChaeSequence)'。

主类。

public class MainActivity extends Activity implements View.OnClickListener
{

EditText userName;
EditText password;
Button loginButton;

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

userName = (EditText) findViewById(R.id.editText);
password = (EditText) findViewById(R.id.editText2);
loginButton = (Button) findViewById(R.id.button);
loginButton.setOnClickListener(this);

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();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
{
return true;
}

return super.onOptionsItemSelected(item);
}

public void onClick(View view)
{
String userStr = userName.getText().toString();
String passStr = password.getText().toString();
Intent intent = new Intent(MainActivity.this, SecondAcivity.class);
intent.putExtra("userName", userStr);
intent.putExtra("password", passStr);
startActivity(intent);
}

二等。

public class SecondAcivity extends MainActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_acivity);

TextView textView = (TextView) findViewById(R.id.textView);
Intent intent = getIntent();
String userName = intent.getStringExtra("userName");
String password = intent.getStringExtra("password");
textView.setText("User Name: " + userName + " Password: " + password);


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();
}
});


}

最佳答案

您不会从 Intent 本身的实例中提取字符串。您需要从 Intent 中获取额外的 Bundle,然后从 Bundle 中获取字符串:

使用这个:

Intent intent = getIntent();
Bundle extras = intent.getExtras();

String userName = "";
String password = "";

if (extras != null) {
userName = extras.getString("userName");
password = extras.getString("password");
}

关于java - 字符串无法在第二个 Activity 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34652656/

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