How to send an object to another activity with special stuff?

别来无恙 提交于 2020-01-06 04:22:24

问题


I am developing a math app for kids. After the user customizes the question options, I want to start another activity. I want to send a QuestionOptions object to the activity so that it can generate questions according to the options. So I created a QuestionnOptions class and implement Parcelable as the other answers showed me. However, there is an enum in my class. So I don't know what to do and let Android Studio generate the stuff needed:

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

Now I send the QuestionOptions to the other 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);

I don't know whether I called the putExtra method correctly, but I continued to write code to get the question options in the other 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 ());

I printed out the options just because I want to see whether it has the correct stuff. But when I run the code, a NullPointerException occurred. Now I really don't know why this is happening. I tried changing the second line to

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

but the result is still the same. I think I must be doing something wrong in the QuestionOptions class, possibly because I didn't write to parcel but there's an enum! There isn't a method called writeEnum()! So can you tell me what I did wrong and how to fix it? And if I did something else wrong please tell me as well.

EDIT:

Here is my logcat:

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

The exception occurred when executing the line:

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

回答1:


I've tried your code and it seems the problem is with the parcelable object. Please update the following and it will work:

The constructor that accepts a Parcel object.

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

and the writeToParcel method.

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

You can get the object from the intent in the following way:

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


来源:https://stackoverflow.com/questions/32177391/how-to-send-an-object-to-another-activity-with-special-stuff

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!