gpt4 book ai didi

java - 如何将一个对象发送到另一个具有特殊内容的 Activity ?

转载 作者:太空宇宙 更新时间:2023-11-04 13:31:20 28 4
gpt4 key购买 nike

我正在为 children 开发一个数学应用程序。用户自定义问题选项后,我想开始另一个 Activity 。我想向 Activity 发送一个 QuestionOptions 对象,以便它可以根据选项生成问题。因此,我创建了一个 QuestionnOptions 类并实现了 Parcelable,正如其他答案向我展示的那样。但是,我的类(class)中有一个枚举。所以我不知道该怎么做,让 Android Studio 生成所需的东西:

package com.smartkidslovemaths;

import android.os.Parcel;
import android.os.Parcelable;

public class QuestionOptions implements Parcelable{
protected QuestionOptions(Parcel in) {
digitCount = in.readInt ();
}

public static final Creator<QuestionOptions> CREATOR = new Creator<QuestionOptions> () {
@Override
public QuestionOptions createFromParcel(Parcel in) {
return new QuestionOptions (in);
}

@Override
public QuestionOptions[] newArray(int size) {
return new QuestionOptions[size];
}
};

public QuestionOptions (OperationType operationType, int digitCount, boolean timerEnabled) {
this.operationType = operationType;
this.digitCount = digitCount;
this.timerEnabled = timerEnabled;
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {

}

@Override
public int hashCode () {
int value;
switch (operationType) {
case ADDITION:
value = 0;
break;
case SUBTRACTION:
value = 4;
break;
case ADD_AND_SUB:
value = 8;
case MULTIPLICATION:
value = 12;
break;
default:
value = 0;
break;
}
value += digitCount;
if (timerEnabled)
value += 100;
return value;
}

@Override
public boolean equals(Object o) {
return o.hashCode() == this.hashCode();
}

public enum OperationType {
ADDITION,
SUBTRACTION,
ADD_AND_SUB,
MULTIPLICATION
}

public boolean isTimerEnabled() {
return timerEnabled;
}

public void setTimerEnabled(boolean timerEnabled) {
this.timerEnabled = timerEnabled;
}

public OperationType getOperationType() {
return operationType;
}

public void setOperationType(OperationType operationType) {
this.operationType = operationType;
}

public int getDigitCount() {
return digitCount;
}

public void setDigitCount(int digitCount) {
this.digitCount = digitCount;
}

private OperationType operationType;
private int digitCount;
private boolean timerEnabled;
}

现在我将 QuestionOptions 发送到其他 Activity :

QuestionOptions options = new QuestionOptions (
operation, digitCount,
cbTimerEnabled.isChecked ());
Intent i = new Intent (MainActivity.this, QuestionsActivity.class);
i.putExtra ("com.smartkidslovemaths.option", options);
startActivity (i);

我不知道我是否正确调用了 putExtra 方法,但我继续编写代码以获取其他 Activity 中的问题选项:

Intent i = getIntent ();
options = i.getExtras ().getParcelable ("com.smartkidslovemaths.options");
System.out.println (options.getDigitCount ());
System.out.println (options.getOperationType ());
System.out.println (options.isTimerEnabled ());

我打印出选项只是因为我想看看它是否有正确的内容。但是当我运行代码时,发生了 NullPointerException 。现在我真的不知道为什么会发生这种情况。我尝试将第二行更改为

options = i.getExtras ().getParcelable ("options");

但是结果还是一样。我想我一定在 QuestionOptions 类中做错了什么,可能是因为我没有写入包,但有一个枚举!没有名为 writeEnum() 的方法!那么你能告诉我我做错了什么以及如何解决它吗?如果我做错了什么,也请告诉我。

编辑:

这是我的日志:

08-24 16:14:20.841      530-750/? E/Diag_Lib﹕ [IMS_FATAL]| 251 | 750 |qvp_rtp_handle_signals iRet : 0
08-24 16:14:20.841 530-750/? E/Diag_Lib﹕ [IMS_FATAL]| 238 | 750 |qvp_rtp_handle_signals qpDplMainLoop: Calling imsSignalHandler
08-24 16:14:20.841 530-750/? E/Diag_Lib﹕ [IMS_FATAL]| 155 | 750 |qpdpl:imsSignalHandler: GLobal data NULL or Event list size is 0
08-24 16:14:20.841 530-750/? E/Diag_Lib﹕ [IMS_FATAL]| 243 | 750 |qvp_rtp_handle_signals add read fd : 9
08-24 16:14:22.823 389-389/? E/Parcel﹕ Reading a NULL string not supported here.
08-24 16:14:22.823 389-389/? E/Parcel﹕ Reading a NULL string not supported here.
08-24 16:14:25.836 530-750/? E/Diag_Lib﹕ [IMS_FATAL]| 251 | 750 |qvp_rtp_handle_signals iRet : 0
08-24 16:14:25.836 530-750/? E/Diag_Lib﹕ [IMS_FATAL]| 238 | 750 |qvp_rtp_handle_signals qpDplMainLoop: Calling imsSignalHandler
08-24 16:14:25.836 530-750/? E/Diag_Lib﹕ [IMS_FATAL]| 155 | 750 |qpdpl:imsSignalHandler: GLobal data NULL or Event list size is 0
08-24 16:14:25.836 530-750/? E/Diag_Lib﹕ [IMS_FATAL]| 243 | 750 |qvp_rtp_handle_signals add read fd : 9
08-24 16:14:29.730 7104-7104/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smartkidslovemaths/com.smartkidslovemaths.QuestionsActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
at android.app.ActivityThread.access$600(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:5225)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.smartkidslovemaths.QuestionsActivity.onCreate(QuestionsActivity.java:16)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
            at android.app.ActivityThread.access$600(ActivityThread.java:150)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:213)
            at android.app.ActivityThread.main(ActivityThread.java:5225)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
            at dalvik.system.NativeStart.main(Native Method)
08-24 16:14:30.842 530-750/? E/Diag_Lib﹕ [IMS_FATAL]| 251 | 750 |qvp_rtp_handle_signals iRet : 0
08-24 16:14:30.842 530-750/? E/Diag_Lib﹕ [IMS_FATAL]| 238 | 750 |qvp_rtp_handle_signals qpDplMainLoop: Calling imsSignalHandler
08-24 16:14:30.842 530-750/? E/Diag_Lib﹕ [IMS_FATAL]| 155 | 750 |qpdpl:imsSignalHandler: GLobal data NULL or Event list size is 0
08-24 16:14:30.842 530-750/? E/Diag_Lib﹕ [IMS_FATAL]| 243 | 750 |qvp_rtp_handle_signals add read fd : 9

执行该行时发生异常:

System.out.println (options.getDigitCount ());

最佳答案

我已经尝试过你的代码,问题似乎出在可包裹对象上。请更新以下内容,它将起作用:

接受 Parcel 对象的构造函数。

protected QuestionOptions(Parcel in) {
this.operationType = OperationType.valueOf(in.readString());
this.digitCount = in.readInt();
this.timerEnabled = in.readByte() != 0;
}

和 writeToParcel 方法。

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.operationType.name());
dest.writeInt(this.digitCount);
dest.writeByte((byte) (this.timerEnabled ? 1 : 0));
}

您可以通过以下方式从 Intent 中获取对象:

final QuestionOptions options = getIntent().getParcelableExtra("com.smartkidslovemaths.option");

关于java - 如何将一个对象发送到另一个具有特殊内容的 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32177391/

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