简体   繁体   English

方法中的SharedPreferences

[英]SharedPreferences in a method

I want to edit my sharedpreferences in a method and save a value. 我想在一个方法中编辑我的sharedpreferences并保存一个值。 If I do so, it says: 如果我这样做,它说:

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

I have an other activity where it worked (but the code was in the oncreate method). 我还有其他活动在起作用(但代码在oncreate方法中)。 Here, the code doesn't seem to work. 在这里,代码似乎不起作用。 Here is my code: 这是我的代码:

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();
                }
            }
        });
}

}

The parts within the if statements are the ones throwing the errors. if语句中的部分是引发错误的部分。 Where is my mistake? 我的错误在哪里?

One of the most important things in programming is writing clear code. 编程中最重要的事情之一就是编写清晰的代码。 Don't duplicate code. 不要重复代码。

Here is sample solution: 这是示例解决方案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM