【问题标题】:Shared Preferences on a Null Object Reference Android空对象引用 Android 上的共享首选项
【发布时间】:2015-09-17 11:44:25
【问题描述】:

我是 android 开发的新手,也许这是一个愚蠢的问题,但请帮助我。我在尝试保存 int 值时遇到此错误。

原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)'

这是我的代码

SharedPreferences prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
String value = "com.example.app.value";
int i = prefs.getInt(value, 0);

对于写

prefs.edit().putInt(number, i).apply();

我只想设置一个 SharedPreferences 并想首先阅读它并将其写入 Activity 中。我该如何解决?

编辑

public class MainActivity extends Activity {
SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "myprefs";
public static final  String value = "key";

int i = sharedpreferences.getInt(value, 0);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
}
public void sendMessage(View view) {
    i += 1;    
    SharedPreferences.Editor editor = sharedpreferences.edit();
    editor.putInt(value, i);
    editor.apply();
}

我设法用不同的方式保存了首选项,但我无法在 MainActivity extends Activity 类中读取它。

日志:

原因:java.lang.NullPointerException:尝试在空对象引用上调用接口方法“int android.content.SharedPreferences.getInt(java.lang.String, int)”

【问题讨论】:

  • SharedPreferences prefs = yourActivity.this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
  • ^ yourActivity.this.getSharedPreferences() 与直接调用 getSharedPreferences() 相比没有效果,因为提供的代码已经在活动中;如果您在实用程序类中,您只会这样做(在这种情况下,直接调用 getSharedPreferences() 甚至不会编译)。此外,硬编码包名(这是一个魔术字符串)也是不好的做法,而是调用 getPackageName()。

标签: java android


【解决方案1】:

这是SharedPreferences的示例

在 SharedPreferences 中保存 name

SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(this);
        Editor editor = sharedPreferences.edit();
        editor.putString(key, name);
        editor.apply();

从 SharedPreferences 中获取 name

 SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(this);
        String name = sharedPreferences.getString(key, "default value");

更多详情请访问:http://developer.android.com/training/basics/data-storage/shared-preferences.html

更新代码:

public class MainActivity extends Activity {
SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "myprefs";
public static final  String value = "key";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    int i = sharedpreferences.getInt(value, 0);
    //use the value of i where needed.
}
public void saveMessage(View view) {
    i += 1;    
    SharedPreferences.Editor editor = sharedpreferences.edit();
    editor.putInt(value, i);
    editor.apply();
}

【讨论】:

  • 使用 .apply() 代替 commit()
  • 引起:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.lang.String android.content.Context.getPackageName()'现在得到这个错误
  • @SimbilliDev 请在您的问题帖子中发布您活动的完整代码。我会改正的。
  • 感谢您的帮助。我找到了另一种解决方法,但这个方法也有效
  • 是的,有很多方法可以使用 SharedPreferences。您将在我的回答中提到的链接中获得有关它的更多详细信息。
【解决方案2】:

这个问题是针对“使用 RetroFit 简化网络”安卓课程的。我收到此错误是因为我在 Android 清单中没有 BaseApplication 类...现在一切都很好

【讨论】:

  • 受此错误困扰两天。仅在阅读您的答案后,我注意到我没有将基本应用程序类放在清单中。现在工作正常
【解决方案3】:

你可以这样实现

public class PreferenceClass {
    public static final String PREFERENCE_NAME = "PREFERENCE_DATA";
    private final SharedPreferences sharedpreferences;

    public PreferenceClass(Context context) {
         sharedpreferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
    }

    public int getCount() {
         int count = sharedpreferences.getInt("count", -1);
         return count;
    }

    public void setCount(int count) {
         SharedPreferences.Editor editor = sharedpreferences.edit();
         editor.putInt("count", count);
         editor.commit();
    }

    public void clearCount() {
         SharedPreferences.Editor editor = sharedpreferences.edit();
         editor.remove("count");
         editor.commit();
    }
}

如果没有设置值,getCount() 将返回 -1。

【讨论】:

    猜你喜欢
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    • 2012-09-20
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多