【问题标题】:Getting NullPointerException in SharedPreferences [duplicate]在 SharedPreferences 中获取 NullPointerException [重复]
【发布时间】:2017-08-08 15:35:23
【问题描述】:

我最近开始开发android。在这种情况下,我可能缺乏一些知识。但是我搜索了很多,无法为这个项目制定解决方案。 面临这个问题。我没弄明白。我的 SharedPreferences 类是。

public class PermanentScoreHolder {

public static boolean storeScore(String prefName,float score)
{
    float temp = VarHolder.SHARED_PREFERENCES.getFloat(VarHolder.SUFFIX_PREFERENCES+prefName, 0f);
    if(temp<score)
    {
        VarHolder.EDITOR.putFloat(VarHolder.SUFFIX_PREFERENCES+prefName, score);
        VarHolder.EDITOR.commit();
        return true;
    }
    return false;
}
public static float getScore(String prefName)
{
    return VarHolder.SHARED_PREFERENCES.getFloat(VarHolder.SUFFIX_PREFERENCES+prefName, 0f);
}
}

我的活动课是这样的:

public class ScoreActivity extends Activity {
TextView normal,danger,thunder,zen,rush;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_score);
    normal = (TextView) findViewById(R.id.normalScore);


    normal.setText(PermanentScoreHolder.getScore("1")+"");//Logcat says problem is here.

}
public void onBackPressed() {
    Intent i = new Intent(getBaseContext(),OptionMenuActivity.class);
    startActivity(i);
    finish();
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.score, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
@Override
protected void onPause() {
    super.onResume();
    overridePendingTransition(0, 0);
}
@Override
protected void onResume() {
    super.onResume();
    overridePendingTransition(0, 0);
}}

我的 Logcat 在这里:

FATAL EXCEPTION: main
Process: cafesoft.td.tappingtile, PID: 8740
java.lang.RuntimeException: Unable to start activity ComponentInfo{cafesoft.td.tappingtile/cafesoft.td.tappingtile.ScoreActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'float android.content.SharedPreferences.getFloat(java.lang.String, float)' on a null object reference
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                                       at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                       at android.os.Looper.loop(Looper.java:164)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:6540)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                    Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'float android.content.SharedPreferences.getFloat(java.lang.String, float)' on a null object reference
                                                                       at cafesoft.td.tappingtile.PermanentScoreHolder.getScore(PermanentScoreHolder.java:18)
                                                                       at cafesoft.td.tappingtile.ScoreActivity.onCreate(ScoreActivity.java:98)
                                                                       at android.app.Activity.performCreate(Activity.java:6980)
                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
                                                                       at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
                                                                       at android.os.Handler.dispatchMessage(Handler.java:105) 
                                                                       at android.os.Looper.loop(Looper.java:164) 
                                                                       at android.app.ActivityThread.main(ActivityThread.java:6540) 
                                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                                       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

我不明白这段代码有什么问题。我需要一个解决方案以及为什么会发生这种情况。

【问题讨论】:

  • 但我遇到了一个具体问题,我知道 NullPointerException 是什么,但我无法弄清楚。不明白问题@JFPicard

标签: java android nullpointerexception sharedpreferences


【解决方案1】:

PermanentScoreHolder 类中的 VarHolder.SHARED_PREFERENCES 有一个空引用。

这就是我们获得SharedPreferences 实例的方式:

SharedPreferences pref = context.getSharedPreferences("unique_key", Context.MODE_PRIVATE);

然后从SharedPreferences获取float

pref.getFloat("key", defaultValue);

【讨论】:

  • 你能建议我在这里做什么吗?
  • 我已经更新了答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-21
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多