简体   繁体   中英

Getting “null object reference” error while trying to send an int with Intent

Code on sender Activity

Intent intent = new Intent(this, ScrollingActivity.class);
    intent.putExtra("KEY", 1);
    startActivity(intent);

Code on receiver Activity

int a = getIntent().getExtras().getInt("KEY");

While trying this getting this error log

java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference

Can you help for fix it?

您的错误表明getIntent返回的是null引用,您应该检查在调用getIntent的位置-应该在Activity生命周期的Actvity.onCreate内部或之后调用它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM