gpt4 book ai didi

java - 传递带有 Intent 的 Integer 会导致空对象引用

转载 作者:行者123 更新时间:2023-12-02 08:46:50 25 4
gpt4 key购买 nike

我正在方法中创建一个新 Intent ,将整数值传递给另一个 Activity 。但是,我在接收端收到空对象引用/空指针异常。这是调用方法(Insect是自定义类型,ID为整数)

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;
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);
}

}

这是异常(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) 

为什么我的mIntent.getIntExtra是一个空对象?据我了解,它已正确初始化。

最佳答案

我认为这是因为 Activity 生命周期。您不应该在 Activity 中使用构造函数。只需将您的代码移至 @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);
}

您会遇到异常,因为您试图在创建 Activity 之前获取 Intent 。

关于java - 传递带有 Intent 的 Integer 会导致空对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61018509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com