如何使用特殊的东西将对象发送到另一个活动?

时间:2015-08-24 08:12:20

标签: java android android-activity parcelable

我正在为孩子们开发一个数学应用程序。用户自定义问题选项后,我想开始另一个活动。我想向活动发送一个JFLAGS = -g -cp library.jar -sourcepath . JC = javac .SUFFIXES: .java .class .java.class: $(JC) $(JFLAGS) $*.java CLASSES = \ src/folder1/a.java \ src/folder1/b.java \ src/folder2/c.java \ src/folder2/d.java \ src/folder3/e.java default: classes classes: $(CLASSES:.java=.class) clean: find . -name "*.class" -type f -delete 对象,以便它可以根据选项生成问题。所以我创建了一个QuestionOptions类并实现QuestionnOptions,其他答案显示给我。但是,我班上有一个枚举。所以我不知道该怎么做,让Android Studio生成所需的东西:

Parcelable

现在我将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

我不知道我是否正确调用了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

我打印出选项只是因为我想看看它是否有正确的东西。但是当我运行代码时,发生了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的方法!所以你能告诉我我做错了什么以及如何解决它?如果我做了别的错误,请告诉我。

修改

这是我的logcat:

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

1 个答案:

答案 0 :(得分:0)

我已经尝试过您的代码,似乎问题在于可以使用parcelable对象。 请更新以下内容,它将起作用:

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

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

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