Sorry for the title,
I want to pass an Array of an selfwritten class through an intent.
This is what the Sender Code looks like:
private void publishResults(int result, DienstplanDatenreihe[] row, String action)
{
Intent intent = new Intent(NOTIFICATION);
intent.putExtra(WorkingCode.TABLE, row);
intent.putExtra(WorkingCode.RESULT, result);
intent.putExtra(ACTION,action);
sendBroadcast(intent);
}
This is what the receiver code looks like:
Bundle extras = intent.getExtras();
DienstplanDatenreihe[] row = (DienstplanDatenreihe[]) extras.getSerializable(WorkingCode.TABLE);
And here comes the exception:
Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to HelperCode.DienstplanDatenreihe[]
And i dont know why... anyone else on the internet does it this way.
[Note: My DienstplanDatenreihe contains only Strings and a String[] ]
DienstplanDatenreihe is probably a custom class that does not implement Parcelable and hence Android cannot pass it via an Intent by default. You will have to make your class implement the Parcelable interface.
Related
I have 2 modules in my project namely app and interface.
app has a dependency on interface. Now i want to pass a string from a class of the app module to a class in the interface module.
Lately i have been trying to pass the values using intents and bundles but i am retrieving a null value on the other end.
Codes:
app module:
Class A:
Intent i= new Intent(getApplicationContext(),Interface.class);
i.putExtra("x","test");
startActivity(i);
Class B:
.... onCreate(Bundle savedInstanceState){
Bundle b=getIntent().getExtras();
String value=b.getString ("x");
}
I am getting the value of x null.
in Class B you need to get String like below:
String value=getIntent().getStringExtra("x");
You can use below code to get the data:
Intent intent = getIntent();
String value = intent.getStringExtra("x");
I have an Intent service. In onHandleIntent I want to put an object of type List<Trends> to an Intent, so that I can send this Intent via sendBroadcast as shown in the code below.
The issue I am facing now is when I put the list object to a Bundle and cast it to Parcelable, I receive the below posted error.
My Code:
Intent intentBroadcast = new Intent();
Bundle bundleList = new Bundle();
bundleList.putParcelable("data", (Parcelable) this.mTrendsList); //java.util.ArrayList cannot be cast to android.os.Parcelable
intentBroadcast.putExtra(TwitterTrendsAPIService.CONST_INTENT_KEY, bundleList);
sendBroadcast(intentBroadcast);
The Error:
java.lang.ClassCastException: java.util.ArrayList cannot be cast to android.os.Parcelable
at com.example.pc_amr.twittertrendsnearlocation.services.TwitterTrendsAPIService.onHandleIntent(TwitterTrendsAPIService.java:86)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:1
You can only put objects of classes that implement Parcelable.
For more info read this.
The putParcelable() method is being used wrong here. This method can be used to save a single object of a class that implements the Parcelable interface. To save an array, you need to use putParcelableArrayList() method.
If you need to pass an ArrayList then I'd go with implementing Parcelable
I have a data of custom List type (finalHolder.data)
Earlier I had to send this data to a Fragment constructor it is working fine
Fragment fragment;
fragment = new RouteMap(finalHolder.data);
((Activity)getContext()).getFragmentManager().beginTransaction().replace(R.id.content_frame,fragment).commit();
The Fragment constructor code
public RouteMap() {
// Required empty public constructor
}
public RouteMap(MyListEntity data) {
this.data = data;
}
Activity constructor :
public Map() {
}
public Map(MyListEntity data) {
this.data = data;
}
But now I want this same data to reach a Activity (Map2)
I tried putExtra with intent but Intent but it doesnt work
In Short I want the "finalHolder.data" to be send to Activity constructor while calling it .
If it isnt possible then please suggest solution
Thanks
It is not recommended to use Activity constructor. To send data object to activity you can use Intent extras with Parcelable. For that you need to do the follow way.
Implement your data class from Parcelable
Ex: MyListEntity implements Parcelable
Override the following methods in your data class(MyListEntity) writeToParcel(), readFromParcel()
3.Implement the CREATOR class in your data class(MyListEntity)
Reference link for Parcelable example: passing object to activity
Send the data object using Intent as mentioned below
Intent i = new Intent(this,Activity(Map2).class);
i.putExtra("mylistdata",finalHolder.data);
startActivity(i);
And finally get the same data at your Activity(Map2) onCreate() method as mentioned below
MyListEntity listEntry = getIntent().getParcelableExtra("mylistdata");
Now your object is ready at your second activity.
Hope this will help for you.
To send an object in intent the object's class must be serializable so make your
MyListEntity implements serializable then use the Intent's putExtra(String name, Serializable value) and in the other activity use the intent's getSerializableExtra (String name) but don't forget to cast the returned value
One option could be letting your custom class implement the Serializable interface and then you can pass object instances in the intent extra using the putExtra(Serializable..) variant of the Intent#putExtra() method.
Your data should implement Serializable ie
MyListEntity implements Serializable
//Use this to send the data
intent.putExtra("MyClass", finalHolder.data);
// To retrieve object in second Activity
getIntent().getSerializableExtra("MyClass");
Regarding your Fragment: it is not smart to pass data via c-tor to fragments!
Use arguments mechanism:
Fragment f = new SomeFragment();
Bundle args = new Bundle();
args.putExtra("data", someData);
f.setArguments(args);
And retreive them:
getArguments().getParcelableExtra("data")
More info here.
But (to return to your question), for that, your data have to implement Serialisable (Java approach) or, by Google advised Parcelable (Android style)
More info about Parcelable here.
Implementing Parcelable interface can be boring and error prone, so some cool people created this: http://www.parcelabler.com/
I am trying to pass a 3D-doublearray to another activity. I don know why I get NullpointerException? What is missing?
MainActivity
Intent intent = new Intent(this, DataActivity.class);
Bundle mBundle = new Bundle();
mBundle.putSerializable("list", output_data);
intent.putExtras(mBundle);
startActivity(intent);
DataActivity (reveiver)
Intent intent = new Intent();
double[][][] params = (double[][][]) intent.getExtras().getSerializable("list");
And I know for certain that the 3d-array already is allocated in the MainActivity. I have tested that!
Would be glad if someone has any solution to this and could answer why I get a NullPointerException.
(Edit: NullpointerException at the line double[][][] params = ...)
I'm pretty sure double[][][] doesn't implement Serializable. What you can do is make your own class like so:
public class MyArrayWrapper implements Serializable {
private double[][][] arr;
public MyArrayWrapper(double[][][] value) {
arr = value;
}
public double[][][] getArray() {
return arr;
}
}
You then instantiate that wrapper class and put it as a serializable instead. Then when getting the serializable you do it like this:
MyArrayWrapper wrap = (MyArrayWrapper) intent.getExtras().getSerializable("list");
double[][]][] myArray = wrap.getArray();
Hope this helps!
Maybe you need a class that implements serializable?
Passing data through intent using Serializable
In this example, they are using custom class and List<> to serialize in a bundle. You can create a class with the 3D array inside?
I am trying to pass arraylist from one activity to other using Bundles in OnPostExecute method,i am not able to do so.I am not getting proper error also to typecast or do stuffs to remove error.I am not sure what is wrong here.
***here reminderList is List<GetReminder> reminderList;***
private class AsyncCallWS extends AsyncTask<String, Void, Void> {
#Override
protected Void doInBackground(String... params) {
//Invoke webservice
vaildUserId=WebService.invokeAuthenticateUserWS(loginUserName, loginPassword, "AuthenticateUser");
if(vaildUserId>=0){
reminderList=WebService.invokeHelloWorldWS("GetReminder");
}
return null;
}
#Override
protected void onPostExecute(Void result) {
Bundle bundle = new Bundle();
bundle.putStringArrayList("reminderList", reminderList);
reminderIntent.putExtras(bundle);
startActivity(new Intent(getApplicationContext(), ReminderActivity.class));
startActivity(reminderIntent);
}
To solve your problem, you can use putParcelableArrayListExtra() and getParcelableArrayListExtra() methods which are defined in the Intent class.
1.Make sure your GetReminder class implements Parcelable .
Here is the document of Parcelable and it also contains a typical implementation of Parcelable.
Here is a website which can help you generate an Parcelable implementation of a class automotically.
2.In your onPostExecute() method put extra like this:
//Remember to declare reminderList as ArrayList here.
ArrayList<GetReminder> reminderList = ...;
Intent intent = new Intent(getApplicationContext(), ReminderActivity.class);
intent.putParcelableArrayListExtra("reminderList", reminderList);
startActivity(intent);
Then in your ReminderActivity class get the ArrayList like this:
ArrayList<GetReminder> list = getIntent().getParcelableArrayListExtra("reminderList");
BTW, there is also another way to solve your problem, you can refer my answer here.
The problem is your List<GetReminder> reminderList; is not a String List.
To pass Custom Object List you have to make your Custom class either Serializable or Parcelable. In your case make GetReminder as Serializable or Parcelable.
Then use putExtra() or putSerializable() of Intent to pass Serializable object.
Also some supercilious code I noted from your onPostExecute() as you have written
startActivity(new Intent(getApplicationContext(), ReminderActivity.class));
startActivity(reminderIntent);
will cause creating two activity instance.
so remove first one,
startActivity(new Intent(getApplicationContext(), ReminderActivity.class));