2017-02-01 7 views
0

Добрый вечер. Или в любое время, когда вы читаете это. В Android Studio, когда я разрабатывал первые этапы своего приложения, у меня возникла проблема, и я не могу найти ответ. Я пытаюсь использовать SharedPrefereces для сохранения пользовательского ввода, но когда я пытаюсь редактировать prefereces [editor = SharedPreferences.edit();], он говорит, что edit() - это нестатический метод, а connot - реферирование из статического контекста.Нестатическое редактирование() не может ссылаться на статический контекст

Вот код.

общественного класса 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

Вы должны вызвать метод .edit(), создав объект SharedPreferences. Изменение 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

Большое спасибо! –

 Смежные вопросы

  • Нет связанных вопросов^_^