发送方活动代码
Intent intent = new Intent(this, ScrollingActivity.class);
intent.putExtra("KEY", 1);
startActivity(intent);
接收器活动代码
int a = getIntent().getExtras().getInt("KEY");
尝试此操作时获取此错误日志
java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
你能帮忙解决它吗?
您的错误表明getIntent
返回空引用,您应该检查您在哪里调用 getIntent - 它应该在活动生命周期中Actvity.onCreate
内部或之后调用。