简体   繁体   中英

ClassCastException on getParcelableExtra with Custom Object?

I have CReated a Custom Class called Player, which implements parcelable so i can pass it through my activities in my android application:

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

I removed some of the unrelated code. I have two activities, both of which send and receive information to one another:

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

I Also removed so unrelated code from the activities. Here is the receiving activity:

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

Here is the error im receiving:

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 also implements parcelable, so im not sure if that causes any issues. But i am not receiving Perk as of yet in Activity 2, so im not sure whats going on.

Thank You for your Time :)

It looks like Player isn't a child of Perk in your class hierarchy. Casting from type A to type B only works if A and B have a direct relationship via subclassing and/or implementation. If you want a more detailed answer, consider posting your getIntent() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM