我想在一个方法中编辑我的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();
                }
            }
        });
}

}


if语句中的部分是引发错误的部分。
我的错误在哪里?

最佳答案

编程中最重要的事情之一就是编写清晰的代码。不要重复代码。

这是示例解决方案:

@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 :-)

10-08 14:14