I have an app that I control the Sphero with via the Sphero Android SDK and in my MainActivity, I have the user connect the Sphero and that Sphero is hooked to the variable mRobot. I want to create an Intent which I already have in my MainActivity:
Intent calibrationIntent = new Intent(MainActivity.this, CalibrationActivity.class);
calibrationIntent.putExtra("Robot", mRobot);
startActivity(calibrationIntent);
Here, I put the mRobot variable in the putExtra()
in the CalibrationActivity.class
, I tried to get the extra variables, but I couldn't:
Intent calibrationIntent = getIntent();
Bundle bundle = getIntent().getExtras();
Sphero mRobot = bundle.getString("Robot");
I get an error in the lineSphero mRobot = bundle.getString("Robot");
because I am trying to convert a Robot data type into a string.
How would I go about passing the mRobot
variable through intents so I could modify it in the other activity? I tried to convert to a string in my MainActivity using mRobot.toString();
, but I don't know how to convert it back into a robot
in the calibrationActivity
.
EDIT:
I tried using getParcelable()
and getSerializable()
in my MainActivity
and in my CalibrationActivity
, I cast the string into a Sphero via
Sphero mRobot = (Sphero) bundle.getParcelable("Robot");
or
Sphero mRobot = (Sphero) bundle.getSerializable("Robot");
But as soon as I press the button after the Sphero is connected, the app crashes, and the log is Caused by: java.lang.ClassCastException: orbotix.robot.base.Robot cannot be cast to orbotix.sphero.Sphero