gpt4 book ai didi

java - 方法中的 SharedPreferences

转载 作者:行者123 更新时间:2023-11-30 02:11:51 24 4
gpt4 key购买 nike

我想在一个方法中编辑我的共享首选项并保存一个值。如果我这样做,它会说:

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

我有一个其他 Activity 可以正常工作(但代码在 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 :-)

关于java - 方法中的 SharedPreferences,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29990795/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com