英文:
Passing an Integer with an Intent results in a null Object reference
问题
以下是翻译好的部分:
调用方法:
private void openInsectDetails(Insect insect){
Intent intent = new Intent(InsectCatchable.this, InsectDetails.class);
intent.putExtra("InsectDetails", insect.getId());
InsectCatchable.this.startActivity(intent);
}
接收类:
package com.ac_companion.wegner;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
public class InsectDetails extends AppCompatActivity {
private int insectPositionMarker;
private Insect localInsect;
public int insectId;
public InsectDetails(){
Intent mIntent = getIntent();
insectId = mIntent.getIntExtra("InsectDetails", 0);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insect_details);
localInsect = Util.getGlobalInsectList().get(insectId);
}
}
异常信息:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ac_companion.wegner, PID: 16344
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ac_companion.wegner/com.ac_companion.wegner.InsectDetails}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference
at com.ac_companion.wegner.InsectDetails.<init>(InsectDetails.java:20)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
...
为什么我的 mIntent.getIntExtra
是空对象?根据我理解,它已经正确初始化了。
英文:
I am ceating a new intent in a method to pass an Integer value to another activity. However, I get a Null Object Reference / Null Pointer Exception on the receiving end.
This is the calling method (Insect is a custom type with an ID that is an integer)
private void openInsectDetails(Insect insect){
Intent intent = new Intent(InsectCatchable.this, InsectDetails.class);
intent.putExtra("InsectDetails", insect.getId());
InsectCatchable.this.startActivity(intent);
}
This is the receiving class:
package com.ac_companion.wegner;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import java.util.ArrayList;
public class InsectDetails extends AppCompatActivity {
private final String TAG = "InsectDetails";
private int insectPositionMarker;
private Insect localInsect;
public int insectId;
//private Intent mIntent = new Intent();
public InsectDetails(){
Intent mIntent = getIntent();
insectId = mIntent.getIntExtra("InsectDetails", 0);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insect_details);
localInsect = Util.getGlobalInsectList().get(insectId);
}
}
This is the Exception:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ac_companion.wegner, PID: 16344
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ac_companion.wegner/com.ac_companion.wegner.InsectDetails}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference
at com.ac_companion.wegner.InsectDetails.<init>(InsectDetails.java:20)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
Why is my mIntent.getIntExtra
a null object? To my unterstanding, its properly initialized.
答案1
得分: 1
我认为这是由于活动生命周期的原因。您不应在活动中使用构造函数。只需将您的代码移到 @OnCreate
方法中。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insect_details);
Intent mIntent = getIntent();
insectId = mIntent.getIntExtra("InsectDetails", 0);
localInsect = Util.getGlobalInsectList().get(insectId);
}
您之所以会得到异常是因为您试图在活动创建之前获取意图。
英文:
I think it's because of activity lifecycle. You shouldn't use a constructor in activity. Just move your code in @OnCreate
methode
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insect_details);
Intent mIntent = getIntent();
insectId = mIntent.getIntExtra("InsectDetails", 0);
localInsect = Util.getGlobalInsectList().get(insectId);
}
You get an exception because you trying to get intent before activity is created.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论