gpt4 book ai didi

java - 无法在自定义上下文包装器注入(inject)中获取共享首选项

转载 作者:行者123 更新时间:2023-12-01 14:07:58 25 4
gpt4 key购买 nike

我正在尝试将用户所需的语言存储在共享首选项中,并使用它通过 this 设置应用程序的语言回答。

我有与该答案相同的 MyConxtextWrapper 文件,但已将包装器注入(inject)更改为:

    @Override
protected void attachBaseContext(Context newBase) {
SharedPreferences sharedPref = getSharedPreferences("userLang", Context.MODE_PRIVATE);
String lang = sharedPref.getString("lang", "");


super.attachBaseContext(MyContextWrapper.wrap(newBase,lang));
}
}

这给了我错误:
04-01 18:35:25.648 25032-25032/com.example.c1629177.cymruniapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.c1629177.cymruniapp, PID: 25032
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.c1629177.cymruniapp/com.example.c1629177.cymruniapp.MainActivity}: 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:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
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:171)
at com.example.c1629177.cymruniapp.MainActivity.attachBaseContext(MainActivity.java:194)
at android.app.Activity.attach(Activity.java:6175)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2350)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

如何将共享首选项值“lang”传递到 super.attachBaseContext(MyContextWrapper.wrap(newBase,lang)); 行?

最佳答案

您正在覆盖 attachBaseContext()它用于将基本上下文附加到您的包装器。在您调用getSharedPreferences()的那一刻基本上下文尚未设置,因此您正在尝试获取 NullPointerException .

因此,请直接在 newBase 上调用此方法语境:

@Override
protected void attachBaseContext(Context newBase) {
SharedPreferences sharedPref = newBase.getSharedPreferences("userLang", Context.MODE_PRIVATE);
String lang = sharedPref.getString("lang", "");
super.attachBaseContext(MyContextWrapper.wrap(newBase, lang));
}

并且可能使用有用的语言默认值;而不是 "" .

关于java - 无法在自定义上下文包装器注入(inject)中获取共享首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43160062/

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