无法实现Parcelable,因为我无法使CREATOR字段静态

Jpa*_*ish 6 java android parcelable

Parcelable文件说,该CREATOR字段必须是静态的,但是当我尝试实施这种方式我得到错误"内部类不能有静态的声明".

我试图通过将我CategoryButton放在一个单独的类(不将其声明为内部类MainActivity)来解决这个问题,但后来我无法调用getApplicationContext()构造函数来传递给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
Run Code Online (Sandbox Code Playgroud)

Dim*_*nov 3

您需要将您的设置CategoryButton为内部静态,即

private static class CategoryButton extends Button implements Parcelable {
    ...
Run Code Online (Sandbox Code Playgroud)