BadParcelableException: Launcher shortcut - java

I'm trying to pass in a custom Object into a shortcut I'm installing on the home screen, but I'm receiving a BadParcelableException when as soon as the shortcut is created. I've tried calling Bundle.setClassLoader, but the error persists.
Is there just a limitation with launchers that support home screen shortcuts?
public class Foo implements Parcelable {
public String title;
private Foo(Parcel in) {
title = in.readString();
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(title);
}
public static final Parcelable.Creator<Foo> CREATOR = new Parcelable.Creator<Foo>() {
#Override
public Foo createFromParcel(Parcel in) {
return new Foo(in);
}
#Override
public Foo[] newArray(int size) {
return new Foo[size];
}
};
}
public static void installShortcut(Context context, Foo foo) {
final Bundle args = new Bundle();
args.setClassLoader(Foo.class.getClassLoader());
args.putParcelable(EXTRA_FOO, foo);
final Intent detail = new Intent(context, FooActivity.class);
detail.putExtras(args);
final Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, detail);
...
context.sendBroadcast(shortcut);
}

Related

Problems with parcelable custom object - Android

I am trying to pass a custom object from one activity to another one.
I found that we can use a bundle to pass data into the intent and that we need a parcelable interface implemented in our class.
In the following code I removed the useless stuff.
public class TravelCard implements Parcelable {
public static final Creator<TravelCard> CREATOR = new Creator<TravelCard>() {
#Override
public TravelCard createFromParcel(Parcel in) {
return new TravelCard(in);
}
#Override
public TravelCard[] newArray(int size) {
return new TravelCard[size];
}
};
private String travelTitle, travelCountry, dateRange, numOfPerson;
private List<TravelDay> days;
protected TravelCard(Parcel in) {
travelTitle = in.readString();
travelCountry = in.readString();
dateRange = in.readString();
numOfPerson = in.readString();
in.readTypedList(days, TravelDay.CREATOR);
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.travelTitle);
dest.writeString(this.travelCountry);
dest.writeString(this.dateRange);
dest.writeString(this.numOfPerson);
dest.writeTypedList(this.days);
}
In this class I have a List of another custom object:
public class TravelDay implements Parcelable {
public static final Creator<TravelDay> CREATOR = new Creator<TravelDay>() {
#Override
public TravelDay createFromParcel(Parcel in) {
return new TravelDay(in);
}
#Override
public TravelDay[] newArray(int size) {
return new TravelDay[size];
}
};
public int current_day;
private String title, note, dateOfToday;
protected TravelDay(Parcel in) {
title = in.readString();
note = in.readString();
dateOfToday = in.readString();
current_day = in.readInt();
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(title);
dest.writeString(note);
dest.writeString(dateOfToday);
dest.writeInt(current_day);
}
I think until here everything should be fine.
In my activity :
Intent intent = new Intent(MainActivity.this, TravelDayActivity.class);
Bundle data = new Bundle();
data.putParcelable(DataKey.TRAVEL_CARD_BUNDLE,item);
intent.putExtras(data);
startActivity(intent);
And then in the TravelDayActivity :
Intent intent = getIntent();
Bundle data = intent.getExtras();
TravelCard travelCard = data.getParcelable(DataKey.TRAVEL_CARD_BUNDLE);
Every time I tried to access the TravelDayActivity the app stop running.
I used the debug mode to search something wrong but I did not find anything.
Thank you in advance
EDIT :
In the MainActivity that's how I get the item variable:
mAdapter = new TravelCardAdapter(travelCards, new TravelCardAdapter.OnItemClickListener() {
#Override
public void onItemClick(TravelCard item) {
Intent intent = new Intent(MainActivity.this,TravelDayActivity.class);
Bundle data = new Bundle();
data.putParcelable(DataKey.TRAVEL_CARD_BUNDLE,item);
intent.putExtras(data);
startActivity(intent);
}
});
Whenever I click on one of the recycler view item this code above is executed.
After the suggestion of #BVantur, I solved my problem using the parceler library :
parceler library
It has been very helpful, easy to use and a good documentation! I highly recommend it.

Extended (custom) arrayList of custom objects parcelable return size 0

It's one day work with no solution,
I would to passed data beetween activity, I implement Parcelable but from called activity I receive the ListFileMedia with a size=0.
When I mistake?
Class ListFileMedia (Extended of ArrayList)
public class ListFileMedia extends ArrayList<FileMedia> implements Parcelable{
public ListFileMedia(){.....}
// for test i have cancel all methods and var, sure i tested also with...
//Interface Parcelable
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
//dest.writeInt(countIntestazioni);
// Add inner class
dest.writeParcelable(this.add(FileMedia),flags);
}
public ListFileMedia(Parcel in) {
}
public final static Parcelable.Creator <ListFileMedia>CREATOR = new Creator<ListFileMedia>() {
#Override
public ListFileMedia createFromParcel(Parcel source) {
return new ListFileMedia(source);
}
#Override
public ListFileMedia[] newArray(int size) {
return new ListFileMedia[size];
}
};
}
Custom objects in FileListMedia
public class FileMedia implements Comparable,Parcelable{
public String getPath() {return path;}
public String getBucket() {
return bucket;
}
public String getMimeType() { return mimeType; }
private String path;
private String bucket;
private String mimeType;
private int giorno;
private int mese;
private int anno;
public FileMedia(int giorno,int mese,int anno, String path,String bucket,String mimeType){
....
}
public int compareTo(Object o) {
.....
}
//Interface Parcelable
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.path);
dest.writeString(this.bucket);
dest.writeString(this.mimeType);
dest.writeInt(this.giorno);
dest.writeInt(this.mese);
dest.writeInt(this.anno);
}
public FileMedia(Parcel in) {
this.path = in.readString();
this.bucket=in.readString();
this.mimeType=in.readString();
this.giorno=in.readInt();
this.mese=in.readInt();
this.anno=in.readInt();
}
public final static Parcelable.Creator <FileMedia>CREATOR = new Creator<FileMedia>() {
#Override
public FileMedia createFromParcel(Parcel source) {
return new FileMedia(source);
}
#Override
public FileMedia[] newArray(int size) {
return new FileMedia[size];
}
};}
Call activity
ListFileMedia test=new ListFileMedia();
ArrayList arrayList=new ArrayList();
test.add(new FileMedia(10,12,2016,"t","t","t"));
b.putParcelable("list",test);
intent.putExtras(b);
startActivity(intent);
Called activity
Intent intent = this.getIntent();
Bundle b=intent.getExtras();
ListFileMedia listFileMedia (ListFileMedia)b.getParcelable("list"); //size==0 !!!
But if i passed only b.putParcelable("list",new FileMedia(10,12,2016,"t","t","t") work correctely!!
Sorry for my English..

Multiple putextra Unmarshalling unknown type code

I want to pass a custom object between my activities. I implemented Parceable for this class and put it in a extra. It works like a charm. Except if I put some other extras. In this case the app crash with this error : Unmarshalling unknown type code.
I have 2 class : A and B. A has a private field B. et my class B has a nested enum Enum. I implemented Parceable for A, B and Enum.
There is my code, I have rewritten it to be more concise.
A.java
public class A implements Parcelable {
B objectB;
String myString;
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(myString);
out.writeParcelable(objectB, flags);
}
public static final Parcelable.Creator<Adresse> CREATOR = new Parcelable.Creator<Adresse>() {
public A createFromParcel(Parcel in) {
return new Adresse(in);
}
public A[] newArray(int size) {
return new A[size];
}
};
private Adresse(Parcel in) {
myString = in.readString();
objectB = in.readParcelable(B.class.getClassLoader());
}
}
B.java
public class B implements Parceable {
Bitmap bitmap;
String otherString;
Enum enum;
public enum Enum implements Parceable {
ONE ("One")
TWO ("Two")
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(ONE.name);
out.writeString(TWO.name);
}
public static final Parcelable.Creator<Identifiant> CREATOR = new Parcelable.Creator<Enum>() {
public Enum createFromParcel(Parcel in) {
return Enum.values()[in.readInt()];
}
public Enum[] newArray(int size) {
return new Enum[size];
}
};
}
public B(Parcel in) {
readFromParcel(in);
}
#Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(otherString);
out.writeParcelable(bitmap, flags);
out.writeParcelable(enum, flags);
}
private void readFromParcel(Parcel in) {
otherString = in.readString();
bitmap = in.readParcelable(Bitmap.class.getClassLoader());
enum = in.readParcelable(Enum.class.getClassLoader());
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public B createFromParcel(Parcel in) {
return new B(in);
}
public B[] newArray(int size) {
return new B[size];
}
};
#Override
public int describeContents() {
return 0;
}
MyActivity.java
Intent intent = new Intent(A.this, B.class);
Bundle bundle = new Bundle();
A objectA = new A();
bundle.putParcelable("A", a);
bundle.putInt("Int", 1); //If I comment this line it works
intent.putExtras(bundle);
startActivityForResult(intent, 1);

How can I pass object from ArrayList to new Activity and then receive that? [duplicate]

This question already has answers here:
How to pass ArrayList<CustomeObject> from one activity to another? [duplicate]
(3 answers)
Closed 7 years ago.
I try pass an object to new activity but I have a problem. My ArrayList use another class in this way:
ArrayList<ListData> myList = new ArrayList<>();
All is great, I'm adding to mList a few objects and my listview work fine.
I try pass an object after click in this way:
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
ListData listdata = myList.get(position);
ListData dataToSend = new ListData();
dataToSend.setStrefa(listdata.getStrefa());
dataToSend.setDzielnica(listdata.getDzielnica());
dataToSend.setAdres(listdata.getAdres());
dataToSend.setKryteria(listdata.getKryteria());
dataToSend.setTelefon(listdata.getTelefon());
dataToSend.setData(listdata.getData());
Intent intent = new Intent(Zlecenia.this, Zlecenie.class);
intent.putExtra("myData", dataToSend);
startActivity(intent);
overridePendingTransition(R.anim.fade_in, R.anim.no_animation);
}
});
Here is a problem because "bundle.putParcelableArrayList("listdata", listdata);" is marked on red. This forces on me using extends Parcelable in ListData Class but when I added this extends then my ArrayList is empty. What schould I do?
My ListData:
public class ListData implements Parcelable{
String Strefa;
String Adres;
String Kryteria;
String Telefon;
String Data;
String Dzielnica;
String Ilosc;
/* Zlecenia */
public String getStrefa() {
return Strefa;
}
public void setStrefa(String strefa) {
this.Strefa = strefa;
}
public String getAdres() {
return Adres;
}
public void setAdres(String adres) {
this.Adres = adres;
}
public String getKryteria() {
return Kryteria;
}
public void setKryteria(String kryteria) {
this.Kryteria = kryteria;
}
public String getTelefon() {
return Telefon;
}
public void setTelefon(String telefon) {
this.Telefon = telefon;
}
public String getData() {
return Data;
}
public void setData(String data) {
this.Data = data;
}
/* Statystyki */
public String getDzielnica() {
return Dzielnica;
}
public void setDzielnica(String dzielnica) {
this.Dzielnica = dzielnica;
}
public String getIlosc() {
return Ilosc;
}
public void setIlosc(String ilosc) {
this.Ilosc = ilosc;
}
public ListData() {
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(Strefa);
dest.writeString(Dzielnica);
dest.writeString(Adres);
dest.writeString(Kryteria);
dest.writeString(Telefon);
dest.writeString(Data);
}
private ListData(Parcel in) {
Strefa = in.readString();
Dzielnica = in.readString();
Adres = in.readString();
Kryteria = in.readString();
Telefon = in.readString();
Data = in.readString();
}
public static final Parcelable.Creator<ListData> CREATOR
= new Parcelable.Creator<ListData>() {
#Override
public ListData createFromParcel(Parcel in) {
return new ListData(in);
}
#Override
public ListData[] newArray(int size) {
return new ListData[size];
}
};
}
Try this in your class
public class ListData implements Serializable

Passing multiple Objects between activities (using parcelable)

So what I am trying to do is pass ObjectB from the first activity to the second. ObjectB contains another object, ObjectA. I am getting really confused as to how to implement this. Also, I am using a list in ObjectA because I am going to have multiple integer values instead of just the 1 in this example. Here is the first activity:
private ObjectB objB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
objB = new ObjectB();
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("KEY", objB);
startActivity(intent);
}
and the second activity:
private ObjectB objB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
objB = new ObjectB();
TextView tv = (TextView) findViewById(R.id.TextView1);
Bundle b = getIntent().getExtras();
objB = b.getParcelable("KEY");
tv.setText(objB.obj.stage1Count);
}
Class for ObjectB:
public class ObjectB implements Parcelable{
public ObjectA obj;
public ObjectB() { obj = new ObjectA(); }
public ObjectA getObj() {
return obj;
}
public ObjectB(Parcel in) {
readFromParcel(in);
}
public void setObj(ObjectA obj) {
this.obj = obj;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(obj, flags);
}
private void readFromParcel(Parcel in) {
obj = in.readParcelable(ObjectA.class.getClassLoader());
}
public static final Parcelable.Creator CREATOR =
new Parcelable.Creator() {
public ObjectB createFromParcel(Parcel in) {
return new ObjectB(in);
}
public ObjectB[] newArray(int size) {
return new ObjectB[size];
}
};
}
Class for ObjectA:
public class ObjectA implements Parcelable{
public int stage1Count = 1;
public ObjectA() { ; };
public ObjectA(Parcel in) {
readFromParcel(in);
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
List<Integer> iList = new ArrayList<Integer>();
iList.add(stage1Count);
dest.writeList(iList);
}
private void readFromParcel(Parcel in) {
List<Integer> iList = new ArrayList<Integer>();
iList = in.readArrayList(Integer.class.getClassLoader());
stage1Count = iList.get(0);
}
public static final Parcelable.Creator CREATOR =
new Parcelable.Creator() {
public ObjectA createFromParcel(Parcel in) {
return new ObjectA(in);
}
public ObjectA[] newArray(int size) {
return new ObjectA[size];
}
};
}
Try this in first activity:
List<ObjectB> list = new ArrayList<ObjectB>;
intent.putParcelableArrayListExtra("key", list);
And in second activity
List<ObjectB> list = new ArrayList<ObjectB>;
list = bundle.getParcelableArrayList("key");
From list get your objectB
This was a really dumb question but I figured it out after a really long time haha. The problem is tv.setText(objB.obj.stage1Count);because objB.obj.stage1Count is actually an integer but for some reason it does not ring any bells when you try to use it as a string, idk maybe its something with the parceing.

Categories

Resources