通过Intent传递整数会导致空对象引用。

huangapple go评论80阅读模式
英文:

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(&quot;InsectDetails&quot;, 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 = &quot;InsectDetails&quot;;
private int insectPositionMarker;
private Insect localInsect;
public int insectId;
//private Intent mIntent = new Intent();
public InsectDetails(){
Intent mIntent = getIntent();
insectId = mIntent.getIntExtra(&quot;InsectDetails&quot;, 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 &#39;int android.content.Intent.getIntExtra(java.lang.String, int)&#39; 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 &#39;int android.content.Intent.getIntExtra(java.lang.String, int)&#39; on a null object reference
at com.ac_companion.wegner.InsectDetails.&lt;init&gt;(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)&#160;
at android.app.ActivityThread.-wrap12(ActivityThread.java)&#160;
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)&#160;
at android.os.Handler.dispatchMessage(Handler.java:102)&#160;
at android.os.Looper.loop(Looper.java:154)&#160;
at android.app.ActivityThread.main(ActivityThread.java:6077)&#160;
at java.lang.reflect.Method.invoke(Native Method)&#160;
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)&#160;
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)&#160;

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(&quot;InsectDetails&quot;, 0);
localInsect = Util.getGlobalInsectList().get(insectId);
}

You get an exception because you trying to get intent before activity is created.

huangapple
  • 本文由 发表于 2020年4月4日 02:44:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/61018509.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定