gpt4 book ai didi

android - SharedPreferences Android 中的 NullPointerException

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:31:04 26 4
gpt4 key购买 nike

我第一次使用 sharedPreferences,我似乎无法克服这个错误。我有一个子菜单应该允许用户设置他们的区域。这应该会打开正确的区域 Activity ,并在再次打开应用程序时存储和调用。我一直在兜圈子很多次,所以有些代码会有点奇怪。我一直专注于从美国(默认)更改为英国。

在 DDMS 中我得到这个:

05-13 11:22:39.344: ERROR/AndroidRuntime(960): java.lang.NullPointerException
05-13 11:22:39.344: ERROR/AndroidRuntime(960): at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146)
05-13 11:22:39.344: ERROR/AndroidRuntime(960): at com.silifeform.android.Prefs.editRegion(Prefs.java:29)
05-13 11:22:39.344: ERROR/AndroidRuntime(960): at com.silifeform.android.dog.onOptionsItemSelected(dog.java:344)

我的代码是这样的:

public class Prefs extends Activity {
public static final String PREFS_NAME="LocalePrefs";
private String region;
public boolean isUk;
public boolean isUs;
public boolean isEu;

@Override
protected void onCreate(Bundle state) {
super.onCreate(state);

//restore prefs
SharedPreferences settings= getSharedPreferences(PREFS_NAME,0);
String myRegion = settings.getString(region,"us");

this.region=myRegion;
changeLocale(getRegion());
}

public void editRegion(String sregion) {
// The error occurs here:
SharedPreferences settings = getSharedPreferences(PREFS_NAME,0);
SharedPreferences.Editor ed = settings.edit();
ed.clear();
ed.putString(region,sregion);
ed.commit();
}

public String getRegion(){
SharedPreferences settings= getSharedPreferences(PREFS_NAME,0);
String myRegion = settings.getString(region,"us");
String gRegion=myRegion;
return gRegion;
}

public void changeLocale(String locale){
try{
String l= locale;
if(l==("us")){
this.isUs=true;
Toast.makeText(this, "Us region P selected", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Prefs.this, dog.class);
startActivity(intent);
}
if(l.equals("uk")){
this.isUk=true;
//Toast.makeText(this, "UK region selected", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Prefs.this, cat.class);
startActivity(intent);
}
}catch (NullPointerException e){
//what to do here?
finish();
}
}

@Override
protected void onStop() {
super.onStop();
SharedPreferences settings = getPreferences(0);
SharedPreferences.Editor ed = settings.edit();
ed.putString(region,region);
ed.commit();

}

我在狗类中的子菜单代码如下所示:

public boolean onOptionsItemSelected(MenuItem item){
Prefs pob = new Prefs();
switch (item.getItemId()){
//-------Options menu----------
case R.id.about:
//Toast.makeText(this, "About menu", Toast.LENGTH_SHORT).show();
//showAbout();
return true;
case R.id.locale:
//Toast.makeText(this, "Locale menu", Toast.LENGTH_SHORT).show();
return true;

//-----Sub menu----------

case R.id.uk_item:
Toast.makeText(this, "UK selected", Toast.LENGTH_SHORT).show();
pob.editRegion("uk");
pob.changeLocale("uk");
finish();
return true;
case R.id.us_item:
Toast.makeText(this, "US already selected", Toast.LENGTH_SHORT).show();
pob.changeLocale("us");
//finish();
return true;
default :
return super.onOptionsItemSelected(item);

}

谢谢

最佳答案

getSharedPreferences() 只能在 onCreate()Activity 调用后调用。您的 Prefs 类奇怪地用在 onOptionsItemSelected() 中。通常,您不会自己实例化 Activity 。如果您的 Prefs 类不是实际的 Activity 而是一个用于访问首选项的辅助类,请将 Context 对象传递给您的方法,如下所示:

public void editRegion(Context ctx, String sregion) {
SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME,0);
SharedPreferences.Editor ed = settings.edit();
ed.clear();
ed.putString(region,sregion);
ed.commit();
}

并通过以下方式调用它:

pob.editRegion(this, "uk");

来自您的 onOptionsItemSelected() 方法。

关于android - SharedPreferences Android 中的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5991289/

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