尝试使用 Intent 发送 int 时出现"null object reference"错误



发送方活动代码

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内部或之后调用。

相关内容

最新更新