0

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

钻头类:

...
    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[]&Parcelable 转换为 Drill[]

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

4

1 回答 1

-1

钻类是抽象的吗?您不能启动抽象类;)

于 2013-11-12T13:45:37.250 回答