我试图在第二个活动中显示用户输入和密码,但是我不幸地登录已经停止了... 我收到一个错误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();
}
});
}
答案 0 :(得分:2)
你不要从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");
}
答案 1 :(得分:1)
当传递空值时,这将处理NullPointerException
试试:
pd.concat
答案 2 :(得分:0)
如果ID textView 存在,请检查 R.layout.activity_second_acivity 。如果您的R.layout.activity_second_acivity中不存在R.id.textView,则texview变量将变为null。
答案 3 :(得分:0)
您尝试将文本设置为null TextView
检查第二个活动布局文件activity_second_acivity
包含textView
答案 4 :(得分:0)
您可以使用此功能从活动中获取字符串
String userName,password;
userName = getIntent().getExtras().getString("userName");
password = getIntent().getExtras().getString("passowrd");
答案 5 :(得分:0)
Bundle extras = getIntent().getExtras();
String userName,password;
if (getIntent().hasExtra("userName")) {
userName = extras.getString("userName");
password = extras.getString("password");
}