gpt4 book ai didi

java - 尽管正在初始化对象,但仍出现空对象引用错误

转载 作者:行者123 更新时间:2023-12-02 09:46:56 25 4
gpt4 key购买 nike

我有 2 个 Activity ,登录和选择语言。选择特定语言后,所选语言将通过共享首选项保存,并且应用程序将通过 Intent 进入登录 Activity 。所有这一切都工作得很好。ChooseLanguage 类中有一个 loadLocale() 方法,我想从 SignIn 类调用该方法。然而,这会产生空指针异常。注意:语言首选项已正确保存,我已经打开它并亲自看到了它。我哪里出错了?

在“选择语言” Activity 中:

public void loadLocale(){
Log.i("TAG","CAME HERE");
SharedPreferences prefs = getSharedPreferences("LanguageSettings", Activity.MODE_PRIVATE);

String preferredLanguage = prefs.getString("PreferredLang","");
setLocale(preferredLanguage);
}

登录 Activity

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ChooseLanguage chooseLanguage = new ChooseLanguage();
chooseLanguage.loadLocale();
setContentView(R.layout.sign_in);
.
.
.
}

我的日志

2019-06-13 11:58:35.302 10533-10533/com.example.gofresh I/TAG: CAME HERE
2019-06-13 11:58:35.304 10533-10533/com.example.gofresh E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.gofresh, PID: 10533
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gofresh/com.example.gofresh.SignIn}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3037)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3172)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1906)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6863)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:174)
at com.example.gofresh.ChooseLanguage.loadLocale(ChooseLanguage.java:103)
at com.example.gofresh.SignIn.onCreate(SignIn.java:64)
at android.app.Activity.performCreate(Activity.java:7149)
at android.app.Activity.performCreate(Activity.java:7140)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1288)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3017)

此处添加了 setLocale() 方法

private void setLocale(String preferredLanguage){
Locale locale = new Locale(preferredLanguage);
Locale.setDefault(locale);
Configuration configuration = new Configuration();
configuration.locale = locale;
getBaseContext().getResources().updateConfiguration(configuration, getBaseContext().getResources().getDisplayMetrics());
if(!preferredLanguage.equals(""))
onConfigurationChanged(configuration);
SharedPreferences.Editor editor = getSharedPreferences("LanguageSettings",MODE_PRIVATE).edit();
editor.putString("PreferredLang",preferredLanguage);
editor.apply();
}


@Override
public void onConfigurationChanged(Configuration newConfig)
{
chooseLanguage.setText(R.string.chooseYourLanguage);
okButton.setText(R.string.ok);
super.onConfigurationChanged(newConfig);

}

最佳答案

您永远不应该自己实例化 Activity 。将其留给操作系统。否则Context将不会被初始化,从而引发NullPointetException。 (Context为null,无法获取SharedPrefs,则SharedPrefs为null)

使其成为当前 Activity 的方法或使其成为沿上下文传递的静态方法,如下所示:

public static void setPreferredLocale(Context ctx) {
SharedPreferences prefs = ctx.getSharedPreferences("LanguageSettings", Activity.MODE_PRIVATE);
String preferredLanguage = prefs.getString("PreferredLang","");
setLocale(preferredLanguage);
}

然后,从任何其他 Activity 调用它,就像

ChooseLanguage.setPreferredLocale(this)

关于java - 尽管正在初始化对象,但仍出现空对象引用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56574659/

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