2017-02-01 98 views
0

晚上好。或者無論什麼時候閱讀。在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)); 
     } 
    }); 



} 

我會明白任何幫助。謝謝!

回答

0

代替該行編輯= SharedPreferences.edit的(); 您應該通過在您的代碼中初始化的sharedpreferences對象調用編輯方法。

+0

謝謝你,以及! –

+0

謝謝!!!!!!!!!! – priyanka

0

您應該通過創建SharedPreferences的對象來調用.edit()方法。在線路改變SharedPreferences,從editor = SharedPreferences.edit();editor = sharedPreferences.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)); 
     } 
    }); 



} 
+0

非常感謝! –

相關問題