I'm working on a simple 'Click Countdown' application, which basically has an imagebutton function. When pressed it displays the number of clicks from 10 to 9, 8, 7,... to 0. I have a problem, when I close the application the number of clicks starts again from 10. I wrote somethink, but it doesn´t work and say: Cannot refer to a non-final variable prefsEditor inside an inner class defined in a different method.-(prefsEditor) Can someone help me please?
This is the code I have so far. I there mistake?
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();
}
}
});
}
}