【发布时间】:2016-05-10 16:36:03
【问题描述】:
我需要将从“活动 A”创建的字符串传递给“活动 B”,以便可以在 TextView 中显示它。 问题是代码导致Android没有响应,它与在线其他教程相同。
感谢您的任何反馈。
活动 A.onCreate()
check_button = (Button) findViewById(R.id.check_button);
check_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Intent i = new Intent(AddActivity.this, DetailActivity.class);
String hash = text_hash.toString();
i.putExtra("hash" , hash);
startActivity(i);
}
});
活动 B.onCreate()
Bundle extras = getIntent().getExtras();
if (extras != null)
{
passedHash = (String) getIntent().getExtras().getString("hash");
hash.setText(passedHash);
}
堆栈跟踪:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
【问题讨论】:
-
text_hash 是 textView 吗?
-
是的,请参阅问题的编辑 :)
-
正如@SajedulKarim 所说,您应该使用 String hash = text_hash.getText().toString();