在SharedPreferences中获取NullPointerException
问题描述:
我最近开始了android开发。在这种情况下,我可能会缺乏一些知识。但是我搜索了很多,无法为此项目提供解决方案。 面对这个问题,我没有弄明白。我的SharedPreferences类是。在SharedPreferences中获取NullPointerException
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)
与此code.I需要一个解决方案,为什么我不明白什么是错这发生。
答
您的VarHolder.SHARED_PREFERENCES
中的类PermanentScoreHolder
具有空引用。
这是我们如何得到SharedPreferences
例如:
SharedPreferences pref = context.getSharedPreferences("unique_key", Context.MODE_PRIVATE);
然后拿到float
从SharedPreferences
:
pref.getFloat("key", defaultValue);
+0
你可以请我建议我在这里做什么? –
+0
我已经更新了我的答案 – Bob
,但我得到我知道什么是空指针异常具体的问题,但虽然我不能图它出来了。不明白这个问题@JFPicard –