It is not possible to implement Parcelable because I cannot make the CREATOR field static

Parcelable say that the CREATOR field should be static, but when I try to implement it this way, I get the error "Inner classes cannot have static declarations."

I tried to resolve this by placing my CategoryButton in a separate class (without declaring it an inner class in MainActivity ), but then I could not call getApplicationContext() in the constructor to go to super.

 public class MainActivity extends ActionBarActivity { private class CategoryButton extends Button implements Parcelable{ private ArrayList<CategoryButton> buttons = null; private RelativeLayout.LayoutParams params = null; public CategoryButton(Context context){ super(context); }; public void setButtons(ArrayList<CategoryButton> buttons){ this.buttons = buttons; } public void setParams(RelativeLayout.LayoutParams params){ this.params = params; } public ArrayList<CategoryButton> getButtons(){ return this.buttons; } public RelativeLayout.LayoutParams getParams(){ return this.params; } public int describeContents() { return 0; } public void writeToParcel(Parcel out, int flags) { out.writeList(buttons); } public static final Parcelable.Creator<CategoryButton> CREATOR // *** inner classes cannot have static declarations = new Parcelable.Creator<CategoryButton>() { public CategoryButton createFromParcel(Parcel in) { return new CategoryButton(in); // *** 'package.MainActivity.this' cannot be referenced from a static context } public CategoryButton[] newArray(int size) { return new CategoryButton[size]; } }; private CategoryButton(Parcel in) { super(getApplicationContext()); in.readList(buttons, null); } } // ...other activity code 
+6
source share
1 answer

You need to set CategoryButton as internal static, i.e.

 private static class CategoryButton extends Button implements Parcelable { ... 
+2
source

All Articles