Android studio 无法从静态上下文引用非静态编辑()

Android studio 无法从静态上下文引用非静态编辑(),android-studio,static,sharedpreferences,Android Studio,Static,Sharedpreferences,晚上好。或者无论你什么时候读这本书。在Android Studio中,当我开发应用程序的第一阶段时,我遇到了一个问题,似乎找不到答案。我试图使用SharedReferences保存用户输入,但当我试图编辑首选项[editor=SharedReferences.edit();]时,它表示edit()是一个非静态方法,不能从静态上下文引用 这是代码 公共类按钮扩展AppCompative活动{ EditText ed1, ed2, ed3, ed4; /*EditText nameInput; E

晚上好。或者无论你什么时候读这本书。在Android Studio中,当我开发应用程序的第一阶段时,我遇到了一个问题,似乎找不到答案。我试图使用SharedReferences保存用户输入,但当我试图编辑首选项[editor=SharedReferences.edit();]时,它表示edit()是一个非静态方法,不能从静态上下文引用

这是代码

公共类按钮扩展AppCompative活动{

EditText ed1, ed2, ed3, ed4;

/*EditText nameInput;
EditText ageInput;
EditText occupationInput;
EditText genderInput;
Button startButtonFinish;*/
protected static final String MyPREFERENCES = "";
public static final String nameInput = "";
public static final int ageInput = 0;
public static final String occupationInput = "";
public static final String genderInput = "";

SharedPreferences sharedpreferences;
SharedPreferences.Editor editor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_the_button);

    ed1 = (EditText) findViewById(R.id.nameInput);
    ed2 = (EditText) findViewById(R.id.ageInput);
    ed3 = (EditText) findViewById(R.id.occupationInput);
    ed4 = (EditText) findViewById(R.id.genderInput);

    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

    Button startButtonFinish = (Button) findViewById(R.id.startButtonFinish);
    startButtonFinish.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String nL = ed1.getText().toString();
            String oI = ed3.getText().toString();
            String gI = ed4.getText().toString();

            //Gives me an error on edit() saying that it edit() is a non-static
            // class and they cannot be referenced from a static context
            editor = SharedPreferences.edit();

            editor.putString(nameInput,nL);
            editor.putString(occupationInput, oI);
            editor.putString(genderInput, gI);
            editor.commit();

            startActivity(new Intent(TheButton.this, thebutton2.class));
        }
    });



}

非常感谢您的帮助。谢谢!

您应该通过创建
SharedReferences
对象调用
.edit()
方法。将
SharedReferences
更改为行中的
editor=SharedReferences.edit();
更改为
editor=SharedReferences.edit();


而不是此行编辑器=SharedReferences.edit();
您应该通过在代码中初始化的SharedReferences对象调用edit方法。

也谢谢您!
EditText ed1, ed2, ed3, ed4;

/*EditText nameInput;
EditText ageInput;
EditText occupationInput;
EditText genderInput;
Button startButtonFinish;*/
protected static final String MyPREFERENCES = "";
public static final String nameInput = "";
public static final int ageInput = 0;
public static final String occupationInput = "";
public static final String genderInput = "";

SharedPreferences sharedpreferences;
SharedPreferences.Editor editor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_the_button);

    ed1 = (EditText) findViewById(R.id.nameInput);
    ed2 = (EditText) findViewById(R.id.ageInput);
    ed3 = (EditText) findViewById(R.id.occupationInput);
    ed4 = (EditText) findViewById(R.id.genderInput);

    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

    Button startButtonFinish = (Button) findViewById(R.id.startButtonFinish);
    startButtonFinish.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String nL = ed1.getText().toString();
            String oI = ed3.getText().toString();
            String gI = ed4.getText().toString();

            //Gives me an error on edit() saying that it edit() is a non-static
            // class and they cannot be referenced from a static context
            editor = sharedPreferences.edit();

            editor.putString(nameInput,nL);
            editor.putString(occupationInput, oI);
            editor.putString(genderInput, gI);
            editor.commit();

            startActivity(new Intent(TheButton.this, thebutton2.class));
        }
    });



}