0

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

rakeshdas
  • 2,363
  • 5
  • 20
  • 28

3 Answers3

1

Okay, I guess I didn't read your problem carefully enough.

I get an error in the line Sphero mRobot = bundle.getString("Robot"); because I am trying to convert a Robot data type into a string.

I didn't catch that you said it was a Robot data type, I just looked at your declaration of mRobot as a Sphero. Now look at your error message:

java.lang.ClassCastException: orbotix.robot.base.Robot cannot be cast to orbotix.sphero.Sphero

The problem is that mRobot is not a Sphero. It is a Robot. Specifically, it is a orbotix.robot.base.Robot.

Change to this:

Robot mRobot = (Robot) bundle.getParcelable("Robot");

assuming you have

import orbotix.robot.base.Robot

I'm not familiar with the type system of the Sphero SDK, so I don't know how Sphero and Robot are related in the type hierarchy, but the error message is telling you: You serialized a Robot and are trying to deserialize it into a Sphero.

kris larson
  • 30,387
  • 5
  • 62
  • 74
0

Whether Sphero is serializable or not, you can always have a shared singleton to pass the data between activities. This won't be limited to any specific type of data. A simple implementation will be like this:

public class DataHolder {
  private Sphero mRobot;
  public Sphero getData() {return mRobot;}
  public void setData(String mRobot) {this.mRobot = mRobot;}

  private static final DataHolder holder = new DataHolder();
  public static DataHolder getInstance() {return holder;}
}

Then in your caller method, do this:

Intent calibrationIntent = new Intent(MainActivity.this, CalibrationActivity.class);
DataHolder.getInstance().setData(mRobot);
startActivity(calibrationIntent);

Then in your CalibrationActivity you can always get the mRobot object by:

Sphero myRobot = DataHolder.getInstance().getData();
Kasra
  • 3,045
  • 3
  • 17
  • 34
0

You are trying to cast an object to a String. Sphero is an Object. You have to pass it as a parcelable extra.

 Sphero mRobot = (Sphero) bundle.getExtra("Robot");
Nick Pontiff
  • 144
  • 5