Robert Simoes Robert Simoes - 6 months ago 23
Android Question

Integer putExtra() in Intent Data is being set as ParcelledData causing AsyncTask exception?

I am running into an error when attempting to getExtras() from an intent. At the moment the Intent Data says its a ParcelledData piece but the argument I entered in the putExtra() method was actually of type int. This causes an exception when during the doIntBackground() process.

Application Context

Flow.java is a container for items called flowElements (aka. Tasks).

A Flow object has a LinkedList which keeps track of all its flowElements.

SandBoxMain.java is an activity where the user can see all the current flowElements in the Flow they are currently working on, or create a flowElement one.

ElementDesigner.java is an activity where the user can name and time their new flowElement.

Error

//// Not expecting a parcel, expecting an integer ////
D/nhacks16.flow.Main.ElementDesigner: intent data is: Bundle[mParcelledData.dataSize=24]
D/nhacks16.flow D/nhacks16.flow.Main.ElementDesigner: Element's name is Task 1
D/nhacks16.flow D/nhacks16.flow.Main.ElementDesigner: Element's time is 20
D/nhacks16.flow D/nhacks16.flow.Main.ElementDesigner: Element's time is minutes
D/nhacks16.flow E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1


Process: nhacks16.flow, PID: 21352
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NumberFormatException: Invalid int: "Bundle[mParcelledData.dataSize=24]"
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parse(Integer.java:410)
at java.lang.Integer.parseInt(Integer.java:367)
at java.lang.Integer.parseInt(Integer.java:334)
at nhacks16.flow.Main.ElementDesigner$newElemAsync.doInBackground(ElementDesigner.java:104)
at nhacks16.flow.Main.ElementDesigner$newElemAsync.doInBackground(ElementDesigner.java:91)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)


SandBoxMain.java

public class SandBoxMain extends AppCompatActivity {
// The SandBox serves as a hub for a Flow Object. SandBox will:
// 1) Execute the elementDesigner activity
// 2) Draw the flowElements on its view

private static final String TAG = SandBoxMain.class.getName();
private Toolbar sbToolbar;
private Flow workingFlow;
// Flow currently being worked on
private FlowElement f;
// Global FlowElement holder

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

workingFlow = getIntent().getParcelableExtra("selectedFlow");
//This parcelable extra comes from a previous activity,
//it is a Flow object, which was selected from a ListView

setContentView(R.layout.activity_sand_box_main);

}

public void newElement(View view) {
//Instantiate a Blank Flow Element
// Add it in the Flow's LinkedList
// Get an id to pass to element designer.
f = new FlowElement();
workingFlow.addElement(f);
Log.d(TAG, "The number of Flow Elements in the current Flow is:"
+ workingFlow.getElementCount());

f.setId(workingFlow.findElement(f));
Log.d(TAG, "The the new blank element's Id is: "
+ f.getId());


Intent in = new Intent(SandBoxMain.this, ElementDesigner.class);

//\\~~~~~~ TROUBLE AREA ~~~~~~~//\\
in.putExtra("id", f.getId());
//Passes the element's id (position in the LinkedList) to the
//Element Designer
\\// ~~~~~~~~~~~~~~~~~~~~~~~~~ \\//

startActivityForResult(in, 1);
//Starts new activity waiting for the return data
}

protected void onActivityResult(int requestCode,
int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);
}
}


ElementDesigner.java

public class ElementDesigner extends AppCompatActivity {
// This class is the activity for the flow element designer, where the
// User can name and time the new task element
// This class will create the object and assign it the inputted properties,
// Then pass them back to the SandboxMain to draw and store

private static final String TAG = ElementDesigner.class.getName();

public void saveElement(View view) {
EditText nameInput = (EditText)findViewById(R.id.nameInput);
EditText timeInput = (EditText)findViewById(R.id.timeInput);
String elementName = nameInput.getText().toString();
String elementTime = timeInput.getText().toString();
String timeUnits = selectTime.getSelectedItem().toString();

//\\ ~~~~~~ TROUBLE AREA ~~~~~~~ //\\
String elementId = getIntent().getExtras().toString();
Log.d(TAG, "intent data is: " + elementId);
\\// ~~~~~~~~~~~~~~~~~~~~~~~~~ \\//

try {
new newElemAsync().execute(elementName, elementTime,
timeUnits, elementId);

}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}

//Runs Async task to create a new FlowElement Object using user's inputs
private class newElemAsync extends AsyncTask<String, String,
FlowElement> {
@Override
protected FlowElement doInBackground(String... params) {

String elementName = params[0];
Log.d(TAG, "Element's name is " + params[0]);

Double elementTime = Double.parseDouble(params[1]);
Log.d(TAG, "Element's time is " + params[1]);

String timeUnits = params[2];
Log.d(TAG, "Element's time is " + params[2]);

//\\ ~~~~~~~~ TROUBLE AREA ~~~~~~~~ //\\
int elementId = Integer.parseInt(params[3]);
Log.d(TAG, "Element's id is " + params[3]);
\\// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \\//

FlowElement newElement = new FlowElement(elementName, elementTime, timeUnits, elementId);

return newElement;
}

@Override
protected void onPostExecute(FlowElement newElem) {
Intent returnData = new Intent();
returnData.putExtra("newElement", newElem);
setResult(1,returnData);
finish();
}
}
}


Flow.java

public class Flow implements Parcelable{
private static final String TAG = Flow.class.getName();
private String name;
private List<FlowElement> childFlowElements = new
LinkedList<FlowElement>();

public int findElement(FlowElement element) {
Log.d(TAG, "The element was found at index: " +
childFlowElements.indexOf(element) +
" in the Flow's childFlowElements LinkedList");

return childFlowElements.indexOf(element);
}

public void addElement(FlowElement newElement) {
childFlowElements.add(newElement);
}
}


edit; please note I removed the OnCreate Methods, for the sake post length!

Answer
 Invalid int: "Bundle[mParcelledData.dataSize=24]"

is the returned value of Bundle.toString(); which is, indeed, not a valid int. It means that in your code, you have

Integer.parseInt(bundle.toString());

E.g. could be the returned value of getIntent().getExtras();

Integer.parseInt(getIntent().getExtras().toString());

You can think of the Bundle as a key-value map. So if you are adding a value using one of its put (putInt) method, you have to use one of its get (getInt) method to retrieve it

Comments