Note that there are some explanatory texts on larger screens.

plurals
  1. POClassCastException on getParcelableExtra with Custom Object?
    text
    copied!<p>I have CReated a Custom Class called Player, which implements parcelable so i can pass it through my activities in my android application:</p> <pre><code>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&lt;Perk&gt; CREATOR = new Parcelable.Creator&lt;Perk&gt;() { 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); } } </code></pre> <p>I removed some of the unrelated code. I have two activities, both of which send and receive information to one another:</p> <p>Activity 1:</p> <pre><code>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&lt;Double&gt; 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&lt;Double&gt;) 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); } }); } } </code></pre> <p>I Also removed so unrelated code from the activities. Here is the receiving activity:</p> <p>Activity 2:</p> <pre><code>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"); }} } </code></pre> <p>Here is the error im receiving:</p> <pre><code>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) </code></pre> <p>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.</p> <p>Thank You for your Time :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload