晚上好。或者无论什么时候阅读。在Android Studio中,当我开发应用程序的第一阶段时,遇到了一个问题,我似乎无法找到答案。我试图使用SharedPrefereces来保存用户输入,但是当我尝试编辑prefereces [editor = SharedPreferences.edit();]时,它说edit()是一个非静态方法,并且不能从静态上下文中引用。非静态编辑()不能从静态上下文中引用
这是代码。
公共类TheButton扩展AppCompatActivity {
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));
}
});
}
我会明白任何帮助。谢谢!
谢谢你,以及! –
谢谢!!!!!!!!!! – priyanka