我有一個應用程序,其中點擊圖像,使由1我的應用與飛濺負載
值(goldCount)增加運行時後「不幸的是,應用程序已停止」對話關閉,應用程序運行的飛濺,在啓動應用程序之前。然而,在飛濺加載後,應用程序關閉,並說「不幸的是,應用程序已停止」,並且我在LogCat中看到這個錯誤列表 - 請參閱this screenshot。
這裏是代碼:
public class MainClass extends Activity implements OnClickListener {
SharedPreferences prefs = getSharedPreferences("LeagueClicker", Context.MODE_PRIVATE);
public float goldCount = 0.0f;
Button minionClick;
TextView textGoldCount;
String textTotal;
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Set fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.mainlayout);
goldCount = prefs.getFloat("goldCount", 0.0f);
//Linking the variables
minionClick = (Button) findViewById(R.id.minioncentreid);
textGoldCount = (TextView) findViewById(R.id.textviewtop);
//String which will display at the top of the app
textTotal = goldCount + " Gold";
//Setting TextView to the String
textGoldCount.setText(textTotal);
textGoldCount.setGravity(Gravity.CENTER);
Typeface tf = Typeface.createFromAsset(getAssets(), "mechanical.ttf");
textGoldCount.setTypeface(tf);
textGoldCount.setTextSize(35);
//Setting onClickListener
minionClick.setClickable(true);
minionClick.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.minioncentreid:
goldCount += 1.0;
prefs.edit().putFloat("goldCount", goldCount).commit();
textTotal = goldCount + " Gold";
textGoldCount.setText(textTotal);
textGoldCount.setGravity(Gravity.CENTER);
break;
}
}
}
我覺得它有什麼做的SharedPreferences,因爲它只有當我試圖執行這些事情。
有誰知道如何解決這個問題?
請問您可以發佈logcat作爲文本?不只是一個圖像? –
嘗試將'getSharedPreferences(「LeagueClicker」,Context.MODE_PRIVATE);'移入'onCreate()'方法 – Delpes
我同意Delpes的建議。 pref在MainClass的第18行給你一個空指針異常,因爲你試圖在onCreate之外實例化它 –