我最近开始开发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)
我不明白这段代码有什么问题。我需要一个解决方案,为什么会这样。
答案 0 :(得分:1)
班级VarHolder.SHARED_PREFERENCES
中的PermanentScoreHolder
有一个空引用。
这就是我们获取SharedPreferences
实例的方式:
SharedPreferences pref = context.getSharedPreferences("unique_key", Context.MODE_PRIVATE);
然后从float
获取SharedPreferences
:
pref.getFloat("key", defaultValue);