NullPointerException异常的共享preferences的Andr​​oid异常、NullPointerException、preferences、oid

2023-09-13 23:48:39 作者:人已成年,心未成年

我第一次使用的共享preferences,我似乎无法让过去的这个错误。我有一个子菜单是应该允许用户设置其区域。这应该打开正确的区域活动,并进行存储和应用程序被再次打开时回忆说。 我一直转圈圈多次使一些code会有点怪异。我一直专注于从美国(默认)英国改变。

My first time using sharedPreferences and i can't seem to get past this error. I have a submenu that is supposed to allow the user to set their region. This should open the correct region activity and be stored and recalled when the app is opened again. I've been going round in circles many times so some of the code will be a bit wierd. I've been focusing on changing from US (default)to UK.

在DDMS我得到这样的:

In the DDMS I'm getting this:

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)

我的code是这样的:

My code is this:

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

}

我的子菜单code在狗类看起来是这样的:

My submenu code in the dog class looks like this:

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

        }

感谢

推荐答案

getShared preferences()可以在的onCreate只能被称为( )一直呼吁的活动。你的 preFS 类奇怪的 onOptionsItemSelected使用()。一般情况下,你不实例化的活动自己。如果你的 preFS 类不是一个实际的活动,而是一个辅助类用于访问preferences,传递上下文反对你的方法,像这样的:

getSharedPreferences() can only be called after onCreate() has been called on an Activity. Your Prefs class is strangely used in onOptionsItemSelected(). In general, you don't instantiate activities yourself. If your Prefs class is not an actual Activity but a helper class for accessing preferences, pass a Context object to your methods, like this:

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()方法。