2015-05-01 20 views
1

我想在一個方法中編輯我的sharedpreferences並保存一個值。如果我這樣做,它說:SharedPreferences中的方法

java.lang.NullPointerException: Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences.edit()' on a null object reference 

我在那裏工作的其他活動(但代碼是在OnCreate方法)。在這裏,代碼似乎不起作用。 這裏是我的代碼:

public class LoginActivity extends ActionBarActivity { 

SharedPreferences myPrefs; 

//some irrelevant code here and there 

public void login(View view){ 
//some more irrelevant code 
bt_SignIn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       String theusername = String.valueOf(username.getText()); 
       String thepass = String.valueOf(pass.getText()); 
       if (theusername.equals("schueler") && thepass.equals("123456")) { 
        SharedPreferences.Editor editor = myPrefs.edit(); 
        editor.putBoolean("LehrerPref", false); 
        editor.apply(); 
        Toast toast = Toast.makeText(getApplicationContext(), "Login erfolgreich! :D", Toast.LENGTH_SHORT); 
        toast.show(); 
        Intent i = new Intent(getApplicationContext(), Frontpage.class); 
        startActivity(i); 
       } else { 
        Toast toast = Toast.makeText(getApplicationContext(), "Login fehlgeschlagen :(", Toast.LENGTH_SHORT); 
        toast.show(); 
       } 
       if (theusername.equals("lehrer") && thepass.equals("14869")) { 
        SharedPreferences.Editor editor = myPrefs.edit(); 
        editor.putBoolean("LehrerPref", true); 
        editor.apply(); 
        Toast toast = Toast.makeText(getApplicationContext(), "Login erfolgreich! :D", Toast.LENGTH_SHORT); 
        toast.show(); 
        Intent i = new Intent(getApplicationContext(), Frontpage.class); 
        startActivity(i); 
       } else { 
        Toast toast = Toast.makeText(getApplicationContext(), "Login fehlgeschlagen :(", Toast.LENGTH_SHORT); 
        toast.show(); 
       } 
      } 
     }); 
} 

} 

內,如果語句是那些投擲錯誤的部分。 我的錯誤在哪裏?

+2

你永遠initalize'myPrefs' – Blackbelt

+0

嗎?怎樣呢? onCreate中的 –

+2

:'myPrefs = getSharedPreferences(「SHARED_PREF_NAME」,MODE_PRIVATE);' – Blackbelt

回答

1

編程中最重要的事情之一是編寫清晰的代碼。不要複製代碼。

這裏是樣品溶液:

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

    bt_SignIn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      String theusername = String.valueOf(username.getText()); 
      String thepass = String.valueOf(pass.getText()); 
      if (theusername.equals("schueler") && thepass.equals("123456")) { 
       setLehrerPref(false); 
       startFrontPage(); 
      } else if (theusername.equals("lehrer") && thepass.equals("14869")) { 
       setLehrerPref(true); 
       startFrontPage(); 
      } else { 
       Toast toast = Toast.makeText(getApplicationContext(), "Login fehlgeschlagen :(", Toast.LENGTH_SHORT); 
       toast.show(); 
      } 
     } 
    }); 
} 

private void setLehrerPref(boolean b){ 
    SharedPreferences sharedPreferences = getSharedPreferences("LehrerPreferences", Context.MODE_PRIVATE); 
    SharedPreferences.Editor editor = sharedPreferences.edit(); 
    editor.putBoolean("LehrerPref", b); 
    editor.apply(); 
} 

private void startFrontPage(){ 
    Toast toast = Toast.makeText(getApplicationContext(), "Login erfolgreich! :D", Toast.LENGTH_SHORT); 
    toast.show(); 
    Intent i = new Intent(getApplicationContext(), Frontpage.class); 
    startActivity(i); 
} 

KISS :-)

相關問題