带自定义对象的getParcelableExtra上的ClassCastException?

时间:2013-12-28 03:05:25

标签: java android

我已经批准了一个名为Player的自定义类,它实现了parcelable,所以我可以通过我的android应用程序中的活动传递它:

public class Player implements Parcelable {

// Perks
public Perk tapUp; // Inc Tap Strength
public Perk minDown; // Dec Tap Decrease
public Perk autoTap; // AutoTap at Fixed rate
public Perk tokUp; // Inc Token Reward
public Perk stoDisc; // Store Discount

// GameMode
public boolean campaign;
public boolean insanity;
public boolean survival;

public Player() {
    this.name = "Steve";
    this.title = "the Granny";
    this.rank = 1;
    this.round = 1;
    this.roundAc = 1;
    this.tokens = 0;
    this.maxTap = 100;
    this.hasTap = 0;
    this.minusTap = 5;
    this.plusTap = 1;
    this.tokenBonus = 0;
    this.tapps = new ArrayList();
    this.tapsPerSec = 0.0;
    this.tapsPerMin = 0.0;
    this.tapsInLife = 0;
    this.playing = false;
    this.secAc = 0;
    this.secT = 0;
    this.minT = 0;
    this.houT = 0;
    this.dayT = 0;
    this.sec = 0;
    this.min = 0;
    this.hou = 0;
    this.day = 0;
    this.tapUp = new Perk();
    this.minDown = new Perk();
    this.autoTap = new Perk();
    this.tokUp = new Perk();
    this.stoDisc = new Perk();
    this.campaign = false;
    this.insanity = false;
    this.survival = false;
}


public Player(Parcel in) {
    name = in.readString();
    title = in.readString();
    rank = in.readInt();
    round = in.readInt();
    roundAc = in.readInt();
    tokens = in.readInt();
    maxTap = in.readDouble();
    hasTap = in.readDouble();
    minusTap = in.readDouble();
    plusTap = in.readDouble();
    tokenBonus = in.readInt();
    // Tapp Array pass through activity
    tapsPerSec = in.readDouble();
    tapsPerMin = in.readDouble();
    tapsInLife = in.readInt();
    // Playing = false
    secAc = in.readInt();
    secT = in.readInt();
    minT = in.readInt();
    houT = in.readInt();
    dayT = in.readInt();
    sec = in.readInt();
    min = in.readInt();
    hou = in.readInt();
    day = in.readInt();

    // Pass Perk In Activity
    // Boolean Changed IN Activity

}

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

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

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(name);
    dest.writeString(title);
    dest.writeInt(rank);
    dest.writeInt(round);
    dest.writeInt(roundAc);
    dest.writeInt(tokens);
    dest.writeDouble(maxTap);
    dest.writeDouble(hasTap);
    dest.writeDouble(minusTap);
    dest.writeDouble(plusTap);
    dest.writeInt(tokenBonus);
    dest.writeDouble(tapsPerSec);
    dest.writeDouble(tapsPerMin);
    dest.writeInt(tapsInLife);
    dest.writeInt(secAc);
    dest.writeInt(secT);
    dest.writeInt(minT);
    dest.writeInt(houT);
    dest.writeInt(dayT);
    dest.writeInt(sec);
    dest.writeInt(min);
    dest.writeInt(hou);
    dest.writeInt(day);
}
}

我删除了一些不相关的代码。我有两个活动,两个活动都相互发送和接收信息:

活动1:

package com.tap.tapalot;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Random;

import perks.Perk;
import player.Player;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class Menu extends Activity {
public Player player;
public ArrayList<Double> tapps;
public Perk tapUp;
public Perk minDown;
public Perk autoTap;
public Perk tokUp;
public Perk stoDisc;

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

    player = new Player();

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        player = (Player) getIntent().getSerializableExtra("player");
        player.tapps = (ArrayList<Double>) getIntent()
                .getSerializableExtra("tapps");
        player.tapUp = (Perk) getIntent().getSerializableExtra("tapUp");
        player.minDown = (Perk) getIntent().getSerializableExtra("minDown");
        player.autoTap = (Perk) getIntent().getSerializableExtra("autoTap");
        player.tokUp = (Perk) getIntent().getSerializableExtra("tokUp");
        player.stoDisc = (Perk) getIntent().getSerializableExtra("stoDisc");
    }

    final TextView plrTokens = (TextView) findViewById(R.id.pltokens);
    String tokStr = String.valueOf(player.tokens);
    plrTokens.setText(tokStr);

    final Button camp = (Button) findViewById(R.id.campaign);

    camp.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent(getBaseContext(), Campaign.class);
            i.putExtra("player", player);
            System.out.println(player.name);
            i.putExtra("tapps", player.tapps);
            i.putExtra("tapUp", player.tapUp);
            i.putExtra("minDown", player.minDown);
            i.putExtra("autoTap", player.autoTap);
            i.putExtra("tokUp", player.tokUp);
            i.putExtra("stoDisc", player.stoDisc);
            startActivity(i);
        }
    });
}
}

我还从活动中删除了这么无关的代码。这是接收活动:

活动2:

package com.tap.tapalot;



public class Campaign extends Activity {
public Player player;
public Player tempPlayer;

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

    player = new Player();
    tempPlayer = new Player();

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        tempPlayer = getIntent()
                .getParcelableExtra("player");
    }}
}

以下是我收到的错误:

12-28 10:53:22.804: E/AndroidRuntime(7446): Caused by: 
java.lang.ClassCastException: perks.Perk cannot be cast to player.Player
12-28 10:53:22.804: E/AndroidRuntime(7446):     
at com.tap.tapalot.Campaign.onCreate(Campaign.java:35)

Perk也实现了parcelable,所以我不确定是否会导致任何问题。但是我还没有在活动2中收到Perk,所以我不知道最近会发生什么。

感谢您的时间:)

1 个答案:

答案 0 :(得分:1)

在您的班级层次结构中,Player似乎不是Perk的孩子。仅当AB通过子类化和/或实现具有直接关系时,才能将A类型转换为类型B。如果您想要更详细的答案,请考虑发布getIntent()方法。