6

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

I attempted to resolve this by putting my CategoryButton in a separate class (not declaring it as an inner class in MainActivity) but then I couldn't call getApplicationContext() in the constructor to pass to the 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
Jpaji Rajnish
  • 1,491
  • 4
  • 17
  • 35
  • I dont understand why you do extend button and implement Parcerable – Trung Nguyen Jul 12 '15 at 01:33
  • Because for my app I need to have a button that has a property `buttons` that contains other buttons... – Jpaji Rajnish Jul 12 '15 at 01:34
  • Why do you need to implement `Parcelable`? – Karakuri Jul 12 '15 at 02:14
  • Because I need to pass the `buttons` property to an `Intent` as an `extra` and it's an `ArrayList` so `CategoryButton` needs to implement `Parcelable` in order for me to call `putParcelableArrayListExtra(String name, ArrayList extends Parcelable> value)` on the `Intent`.before I pass it to `startActivity()`. – Jpaji Rajnish Jul 12 '15 at 02:30

1 Answers1

3

You need to set your CategoryButton as inner static, i.e.

private static class CategoryButton extends Button implements Parcelable {
    ...
Dimitar Genov
  • 2,056
  • 13
  • 11
  • I just tried this and although it fixes the `CREATOR` field problem, it makes it so I can't call `getApplicationContext()` to pass to the super in the constructor. – Jpaji Rajnish Jul 12 '15 at 01:36
  • Button is a view, i.e. change your call to context as `super(getContext());` – Dimitar Genov Jul 12 '15 at 01:38
  • Hmm now I get the error `Cannot reference 'View.getContext()' before supertype constructor has been called` – Jpaji Rajnish Jul 12 '15 at 01:41
  • Well, it is a custom constructor, just replace it with `private CategoryButton(@NonNull Context context, Parcel in) {...`. But depends of your logic, you may need to change the way you are calling this private constructor. – Dimitar Genov Jul 12 '15 at 01:45
  • I just tried changing the signature of the constructor as you describe and then calling it from `createFromParcel()` like this: `return new CategoryButton(getContext(), in);` but then I'm back to the error `Non-static method 'getContext()' cannot be referenced from a static context` :\ – Jpaji Rajnish Jul 12 '15 at 01:55
  • Please look [here](http://stackoverflow.com/questions/3542333/how-to-prevent-custom-views-from-losing-state-across-screen-orientation-changes/3542895#3542895) how to restore from `Parcelable` – Dimitar Genov Jul 12 '15 at 02:02