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

2023-09-12 05:38:23 作者:不合脚

我开发孩子们的数学应用程序。在用户自定义的问题的选项,我要开始另一项活动。我想送一个 QuestionOptions 对象的活动,以便它可以根据选项生成的问题。所以,我创建了一个 QuestionnOptions 类,并实现 Parcelable 作为其他的答案给我看。但是,在我的课枚举。所以我不知道该怎么办,让机器人工作室生成所需要的东西:

 包com.smartkidslovemaths;

进口android.os.Parcel;
进口android.os.Parcelable;

公共类QuestionOptions实现Parcelable {
    保护QuestionOptions(包裹中){
        digitCount = in.readInt();
    }

    公共静态最终的创造者和LT; QuestionOptions> CREATOR =新的创造者和LT; QuestionOptions> (){
        @覆盖
        公共QuestionOptions createFromParcel(包裹中){
            返回新QuestionOptions(中);
        }

        @覆盖
        公共QuestionOptions [] newArray(INT尺寸){
            返回新QuestionOptions【尺寸】;
        }
    };

    公共QuestionOptions(OperationType operationType,INT digitCount,布尔timerEnabled){
        this.operationType = operationType;
        this.digitCount = digitCount;
        this.timerEnabled = timerEnabled;
    }

    @覆盖
    公众诠释describeContents(){
        返回0;
    }

    @覆盖
    公共无效writeToParcel(包裹DEST,INT标志){

    }

    @覆盖
    公众诠释哈希code(){
        int值;
        开关(operationType){
            案例增加:
                值= 0;
                打破;
            案例加减:
                值= 4;
                打破;
            案例ADD_AND_SUB:
                值= 8;
            案例乘法:
                值= 12;
                打破;
            默认:
                值= 0;
                打破;
        }
        值+ = digitCount;
        如果(timerEnabled)
            值+ = 100;
        返回值;
    }

    @覆盖
    公共布尔等于(对象o){
        返回o.hash code()== this.hash code();
    }

    公共枚举OperationType {
        加成,
        减法,
        ADD_AND_SUB,
        乘法
    }

    公共布尔isTimerEnabled(){
        返回timerEnabled;
    }

    公共无效setTimerEnabled(布尔timerEnabled){
        this.timerEnabled = timerEnabled;
    }

    公共OperationType getOperationType(){
        返回operationType;
    }

    公共无效setOperationType(OperationType operationType){
        this.operationType = operationType;
    }

    公众诠释getDigitCount(){
        返回digitCount;
    }

    公共无效setDigitCount(INT digitCount){
        this.digitCount = digitCount;
    }

    私人OperationType operationType;
    私人诠释digitCount;
    私人布尔timerEnabled;
}
 

现在我送了 QuestionOptions 其他活动:

  QuestionOptions选项=新QuestionOptions(
                        操作,digitCount,
                        cbTimerEnabled.isChecked());
意图I =新的意图(MainActivity.this,QuestionsActivity.class);
i.putExtra(com.smartkidslovemaths.option选项);
startActivity(ⅰ);
 

我不知道我是否叫 putExtra 正确的方法,但我继续写code以获得在其他活动的问题选项:

 意图I = getIntent();
选项​​= i.getExtras().getParcelable(com.smartkidslovemaths.options);
的System.out.println(options.getDigitCount());
的System.out.println(options.getOperationType());
的System.out.println(options.isTimerEnabled());
 
支付宝上线消息刷子功能 网友 期待微信上线这个功能

我打印出来,只是因为我想看看它是否有正确的东西的选项。但是,当我运行code,A NullPointerException异常发生。现在我真的不知道为什么会这样。我试图改变第二行

 选项= i.getExtras().getParcelable(「购股权」);
 

但结果还是一样。我想我必须做一些错误的 QuestionOptions 类,可能是因为我没有写信给包裹,但有一个枚举!没有一个叫方法 writeEnum()!所以,你能告诉我,我做错了什么,以及如何解决它?如果我没有别的东西错了,请告诉我为好。

编辑:

下面是我的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:调用imsSignalHandler
08-24 16:14:20.841 530-750 /? E / Diag_Lib:[IMS_FATAL] | 155 | 750 | qpdpl:imsSignalHandler:全球数据NULL或事件列表大小为0
08-24 16:14:20.841 530-750 /? E / Diag_Lib:[IMS_FATAL] | 243 | 750 | qvp_rtp_handle_signals加FD阅读:9
08-24 16:14:22.823 389-389 /? E /包裹:在这里读不支持空字符串。
08-24 16:14:22.823 389-389 /? E /包裹:在这里读不支持空字符串。
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:调用imsSignalHandler
08-24 16:14:25.836 530-750 /? E / Diag_Lib:[IMS_FATAL] | 155 | 750 | qpdpl:imsSignalHandler:全球数据NULL或事件列表大小为0
08-24 16:14:25.836 530-750 /? E / Diag_Lib:[IMS_FATAL] | 243 | 750 | qvp_rtp_handle_signals加FD阅读:9
08-24 16:14:29.730 7104-7104 /? E / AndroidRuntime:致命异常:主要
    java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.smartkidslovemaths / com.smartkidslovemaths.QuestionsActivity}:显示java.lang.NullPointerException
            在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
            在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
            在android.app.ActivityThread.access $ 600(ActivityThread.java:150)
            在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1298)
            在android.os.Handler.dispatchMessage(Handler.java:99)
            在android.os.Looper.loop(Looper.java:213)
            在android.app.ActivityThread.main(ActivityThread.java:5225)
            在java.lang.reflect.Method.invokeNative(本机方法)
            在java.lang.reflect.Method.invoke(Method.java:525)
            在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:741)
            在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
            在dalvik.system.NativeStart.main(本机方法)
     显示java.lang.NullPointerException:产生的原因
            在com.smartkidslovemaths.QuestionsActivity.onCreate(QuestionsActivity.java:16)
            在android.app.Activity.performCreate(Activity.java:5133)
            在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
在android.app.ActivityThread.access $ 600(ActivityThread.java:150)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1298)
在android.os.Handler.dispatchMessage(Handler.java:99)
在android.os.Looper.loop(Looper.java:213)
在android.app.ActivityThread.main(ActivityThread.java:5225)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:525)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:741)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
在dalvik.system.NativeStart.main(本机方法)
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:调用imsSignalHandler
08-24 16:14:30.842 530-750 /? E / Diag_Lib:[IMS_FATAL] | 155 | 750 | qpdpl:imsSignalHandler:全球数据NULL或事件列表大小为0
08-24 16:14:30.842 530-750 /? E / Diag_Lib:[IMS_FATAL] | 243 | 750 | qvp_rtp_handle_signals加FD阅读:9
 

发生

例外执行行时:

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

解决方案

我试过你的code,似乎问题出在parcelable对象。 请更新以下内容,将工作:

这是接受一个包裹对象的构造函数。

 保护QuestionOptions(包裹中){
    this.operationType = OperationType.valueOf(in.readString());
    this.digitCount = in.readInt();
    this.timerEnabled = in.readByte()!= 0;
}
 

和writeToParcel方法。

  @覆盖
公共无效writeToParcel(包裹DEST,INT标志){
    dest.writeString(this.operationType.name());
    dest.writeInt(this.digitCount);
    dest.writeByte((字节)(this.timerEnabled?1:0));
}
 

您可以从意向对象通过以下方式:

 最后QuestionOptions选项= getIntent()getParcelableExtra(com.smartkidslovemaths.option)。
 

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

解决方案

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