我是Android编程的新手,我仍然没有得到这个意图概念。我在其开发网站上查看了android文档,它将意图传递为
public void nothing(View view) //nothing is defined in XML fine as android:onClick="nothing"
//in button
{
Intent intent=new Intent(this, Learning.class);
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
所以,它传递给了Learning.class ..但是,当我想去另一个班而没有传递任何额外的内容时,
public void nothing(View view) //nothing is defined in XML fine as android:onClick="nothing"
//in button
{
Intent intent=new Intent(this, Learning.class);
// intent.putExtra(EXTRA_MESSAGE, message); <--didn't put this one
startActivity(intent);
}
它显示了NULLPOINTEREXCEPTION。
那么,问题是什么?我们不能没有任何put.Extra传递意图吗?
我的学习活动oncreate看起来像这样:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent=getIntent();
setContentView(R.layout.fragment_learning);
答案 0 :(得分:0)
Intent intent = getIntent();
String message = inetnt.getExtras().getString(EXTRA_MESSAGE);
// here is the error coz intent is null
所以,如果intent为null或no,则先检查:
Intent intent = getIntent();
if(intent != null){
String message = inetnt.getExtras().getString(EXTRA_MESSAGE);
}