2017-08-08 4 views
-2

Je commence à développer Android récemment. Je peux avoir un certain manque de connaissance dans ce scénario. Mais j'ai beaucoup cherché et ne peux pas faire une solution pour ce projet. Face à un problème avec ceci.J'ai pas réussi à le comprendre. Ma classe SharedPreferences est.Obtenir NullPointerException dans 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); 
} 
} 

Ma classe d'activité est comme ceci:

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

Mon Logcat est ici:

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) 

Je ne comprends pas ce qui est mal avec ce code.I besoin d'une solution et pourquoi Cela se produit.

+0

mais je reçois un problème spécifique, je sais ce qui est NullPointerException, mais si je ne peux pas Il ne comprend pas le problème @JFPicard –

Répondre

0

Votre VarHolder.SHARED_PREFERENCES dans la classe PermanentScoreHolder a une référence null.

Voici comment nous obtenons SharedPreferences exemple:

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

obtenir ensuite le float de SharedPreferences:

pref.getFloat("key", defaultValue); 
+0

Pouvez-vous s'il vous plaît me suggérer ce qu'il faut faire ici? –

+0

J'ai mis à jour ma réponse – Bob