parcelable对象数组,该类继承自抽象类

时间:2012-12-28 20:00:33

标签: android parcelable

我有一个抽象类Drill和4个扩展它的类(addClass,divClass等)。 Drill类实现了parcelable。现在我有GamePlayActivity需要传递给Result活动一个Drill数组,我有两个编译问题。以下是相关代码:

钻类:

...
    public Drill(Parcel in) {
        readFromParcel(in);
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // We just need to write each field into the
        // parcel. When we read from parcel, they
        // will come back in the same order
        dest.writeInt(firstNumber);
        dest.writeInt(secondNumber);
        dest.writeInt(answer);
        dest.writeInt(userAnswer);
        dest.writeString(userAnswerString);
        dest.writeString(symbol);
        dest.writeInt(minBound);
        dest.writeInt(maxBound);
        dest.writeInt(drillNumber);
    }

    /**
     *
     * Called from the constructor to create this
     * object from a parcel.
     *
     * @param in parcel from which to re-create object
     */
    protected void readFromParcel(Parcel in) {
        // We just need to read back each
        // field in the order that it was
        // written to the parcel
        firstNumber = in.readInt();
        secondNumber = in.readInt();
        answer = in.readInt();
        userAnswer = in.readInt();
        userAnswerString = in.readString();
        symbol = in.readString();
        minBound = in.readInt();
        maxBound = in.readInt();
    }

    public static final Parcelable.Creator<Drill> CREATOR = new Parcelable.Creator<Drill>() {
        public Drill createFromParcel(Parcel in) {
            return new Drill(in); // compilation error
        }

        public Drill[] newArray(int size) {
            return new Drill[size];
        }
    };

}

我认为这里的错误是因为Drill调用是抽象的。

这是addDrill类,另一个类看起来是一样的:

package com.simplemathgame;

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

public class AddDrill extends Drill {

    AddDrill(int min, int max) {
        super(min, max);
        symbol = "+";
        firstNumber = getRandomNumber(min, max);
        secondNumber = getRandomNumber(min, max);
        answer = answerCalculatetion();
    }

    private AddDrill(Parcel in) {
        super(in);
    }

    public void readFromParcel(Parcel in) {
        super.readFromParcel(in);
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        super.writeToParcel(out, flags);
    }

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

    protected int answerCalculatetion() {
        return (this.getFirstNumber() + this.getSecondNumber());
    }
}

这是GamePlayClass,传递数组的类:

package com.simplemathgame;

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class GamePlayActivity extends Activity {
...

    Drill[] arrayOfDrills;
    int dIndex = 0;
...


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_play);

        ...

        arrayOfDrills = new Drill[addDrills + subDrills + mulDrills + divDrills];

        ...

        finishButton.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
            ...
                Intent resultsActivity = new Intent(GamePlayActivity.this, Results.class);
                resultsActivity.putExtra("drillArray", arrayOfDrills);
                startActivity(resultsActivity);
            }

        });
    }

    public static <T> void shuffle(T[] array) {
        for (int i = array.length; i > 1; i--) {
            T temp = array[i - 1];
            int randIx = (int) (Math.random() * i);
            array[i - 1] = array[randIx];
            array[randIx] = temp;
        }
    }
}

最后一部分是接收数组的Result活动:

package com.simplemathgame;

import android.os.Bundle;
import android.app.Activity;

public class Results extends Activity {
    Drill[] arrayOfDrills;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_results);

        Bundle extras  = getIntent().getExtras();
        arrayOfDrills = extras.getParcelable("drillArray"); // compilation error
    }
}

这里我不知道为什么会发生错误。

第一次编译错误: 无法实例化类型Drill

第二次编译错误: 类型不匹配:无法从Drill []&amp; Parcelable转换为Drill []

我该如何修复我的代码?谢谢!

1 个答案:

答案 0 :(得分:-1)

钻类是抽象的吗?你不能发起抽象类;)