-1
我正在開發一個簡單的'Click Countdown'應用程序,它基本上有一個圖像按鈕 函數。點擊次數從10到9,8,7,...顯示爲0 我遇到問題,關閉應用程序時,點擊次數從 開始10.我寫了一些思考,但它沒有工作,並說:不能在一個不同的方法定義的內部類中引用非最終變量prefsEditor。 - (prefsEditor)有人可以幫我嗎?SharedPreferences - 節省按鈕點擊次數
這是我到目前爲止的代碼。我有錯嗎?
package com.example.testapp;
import com.example.testapp.R;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ImageButton;
import android.view.View;
import android.widget.TextView;
import android.view.View.OnClickListener;
import com.google.ads.AdRequest;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import com.google.ads.AdView;
public class MainActivity extends Activity {
ImageButton button1;
TextView textView1;
int counter = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdView adView = (AdView)this.findViewById(R.id.adView);
adView.loadAd(new AdRequest());
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor prefsEditor = prefs.edit();
ImageButton imageButton;
imageButton = (ImageButton) findViewById(R.id.button1);
textView1 = (TextView) findViewById(R.id.textView1);
button1 = (ImageButton) findViewById(R.id.button1);
imageButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
if (counter >= 1) {
counter--;
textView1.setText("" + counter);
prefsEditor.putInt("counter", counter);
prefsEditor.commit();
} else if (counter == 0){
button1.setImageResource(R.drawable.image2);
counter--;
prefsEditor.putInt("counter", counter);
prefsEditor.commit();
}
}
});
}
}
http://stackoverflow.com/questions/1299837/cannot-refer-to-a-non-final-variable-inside-an-inner-class- defined-in-a-differen?lq = 1 – StarsSky