Strange behavior when reading from SharedPreferences - java

when I'm launching my application and the onResume() method is being run, something goes wrong when reading from my SharedPreferences. This is how the code looks.
static double cowCount = 197, income, cowMult = 1;
...
protected void onResume() {
super.onResume();
SharedPreferences sharedPref = getSharedPreferences("com.example.cowcount", Context.MODE_PRIVATE);
cowCount = sharedPref.getFloat("cowCount", 0);
cowMult = sharedPref.getFloat("cowMult", 0);
income = sharedPref.getFloat("income", 0);
}
...
When the code is like this, the application is being frozen. The application consists of a counter, and when I push the button that is supposed to count up, nothing happens.
However, when i comment out the line where i assign a value to the cowMult double from the SharedPreferences, the application doesn't freeze.
cowCount = sharedPref.getFloat("cowCount", 0);
// cowMult = sharedPref.getFloat("cowMult", 0);
income = sharedPref.getFloat("income", 0);
To be clear, the above works fine.
This is the method called when pushing the button (that is supposed to higher the value of cowCount by one):
public void addCow (View view) {
cowCount = cowCount + cowMult;
refresh();
}
...
public void refresh () {
TextView myTextView = (TextView)findViewById(R.id.myText);
myTextView.setText("You Have " + String.valueOf((nf.format(cowCount)) + " Cows!"));
}

Change your code as follows:
static float cowCount, income, cowMult;
...
protected void onResume()
{
super.onResume();
SharedPreferences sharedPref = getSharedPreferences("com.example.cowcount", Context.MODE_PRIVATE);
cowCount = sharedPref.getFloat("cowCount", 197);
cowMult = sharedPref.getFloat("cowMult", 1);
income = sharedPref.getFloat("income", 0);
}
...
The second parameter in SharedPreferences.getFloat() is a default value that the method will return if the key is not found. With the code you've supplied, If you've not saved the values to SharedPreferences properly, those variables will be assigned a value of 0. This is why nothing changes when you press the button; you're adding 0. Check to make sure you are saving to SharedPreferences correctly.
Also, there's no point in initializing the variables when you declare them, as they all get assigned a value in the onResume method, be it a saved value or a default value.
And, as pointed out by Martin, assign your TextView in your onCreate method.

A couple of things are strange with the code you have posted
1 why are you calling
SharedPreferences sharedPref = getSharedPreferences(null, Context.MODE_PRIVATE);
and not
SharedPreferences sharedPref = getSharedPreferences( "com.myname.myapp", Context.MODE_PRIVATE);
2 your onResume should be the following
super.onResume();
SharedPreferences sharedPref = getSharedPreferences(null, Context.MODE_PRIVATE);
cowCount = Double.longBitsToDouble(sharedPref.getLong("cowCount", 0));
cowMult = Double.longBitsToDouble(sharedPref.getLong("cowMult", 0));
income = Double.longBitsToDouble(sharedPref.getLong("income", 0));
calling the super.onResume() first before your code (same with all life cycle methods)
EDIT
3. Why are you not just setting your values to be an int (from what you said above they seem to be) or a float which will give you possibly all the precision then you can just get your values using
getInt(String key, int defValue)
of
getFloat(String key, float defValue)
Seconded Edit
There are a few strange ways you are doing things in the code that I can see. try the following code and let me know if it fixed the problem (though I can't see how the sharedPreferences would be causing it ). i am presuming the addCow method is being called from an onClickListener
//get a reference for your myTextView in the onCreate() method, after declaring your variable
//outside the onCreate method i.e
TextView myTextView;
...
// int onCreate()
myTextView = (TextView)findViewById(R.id.myText);
//you don't need to pass a view parameter, so don't
public void addCow () {
cowCount = cowCount + cowMult;
refresh();
}
...
public void refresh () {
//the way you are getting a string value is also not what I would do either use
//Float.toString(cowCount) or just
myTextView.setText("You Have " + cowCount + " Cows!"));
}
Hope the issue is gone.

Related

How to change text of Text View outside of onCreate()

I am trying to change the text of a TextView on my Activity. It happens in a method in the same class as the onCreate method. However, it does not work. I've googled it but found nothing.
This is the error I got:
Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference.
This is the Main Activity, where my TextView is located:
private static TextView welcome;
private static TextView counter;
private static int number;
private static SharedPreferences sharedPreferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
//Shared Preferences getting called to see if the user has set his name
SharedPreferences sharedpreferences = getSharedPreferences("name", Context.MODE_PRIVATE);
//Getting number
this.number = sharedpreferences.getInt("number", 0);
this.sharedPreferences = sharedpreferences;
Boolean continueName = false;
//Checking if name is set
if (!sharedpreferences.getBoolean("nameSet", false)) {
//If name is not set
Intent intent = new Intent(this, Name.class);
startActivity(intent);
} else {
//if name was already set, default activity pops Up
continueName = true;
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//getting counter and setting it
TextView count = (TextView) findViewById(R.id.txtNumber);
this.counter = count;
//Actionlistener to +1 Button
ActionListeners al = new ActionListeners();
Button plusOne = (Button) findViewById(R.id.btnAddOne);
plusOne.setOnClickListener(al.getPlusOneListener());
//Setting Text View Object
this.welcome = (TextView) findViewById(R.id.welcomeUser);
//If the name is set
if (continueName) {
this.welcome.setText(getString(R.string.welcome) + " " + sharedpreferences.getString("usersName", ""));
}
}
public void setNameNew() throws InterruptedException {
TextView welcomeThis = this.welcome;
new Thread(new Runnable() {
public void run() {
//Setting welcome text
SharedPreferences sp = getSharedPreferences("name", Context.MODE_PRIVATE);
welcomeThis.setText(getString(R.string.welcome) + " " + sp.getString("usersName", ""));
}
});
Thread.sleep(500);
this.welcome.setText(welcomeThis.getText());
}
public void changeViewNumber(int number) {
//Setting new number
this.counter.setText(number);
}
}
Weird is, that the setNameNew method is working and can change the text of the TextField. But the changeViewNumber method is not working.
Im on the activity where the TextView is located. I can't figure it out. May you please help me?
You can call setText for a TextView anywhere in the class as long as the TextView is referenced. When you say one function is working and the other isn't, it's because the reference for the counter is incorrect.
Your counter reference
TextView count = (TextView) findViewById(R.id.txtNumber);
this.counter = count;
Your welcome reference
this.welcome = (TextView) findViewById(R.id.welcomeUser);
You should be referencing the view the way you referenced welcome. You'll want to change the counter reference to the following.
this.counter = (TextView)findViewById(R.id.txtNumber)
Your count variable does nothing and should be removed.
I would also like to make additional notes for your code. You are using the keyword this, it isn't necessary for your code.
The this keyword refers to the current object in a method or constructor.
The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same name (because a class attribute is shadowed by a method or constructor parameter.
I would also strongly recommend not putting the main thread to sleep, if the thread is sleeping and action is required it will cause your app to crash.
This is useless
TextView count = (TextView) findViewById(R.id.txtNumber);
this.counter = count;
Just do
counter = findViewById(R.id.txtNumber);
This is because you've declared welcome variable globally in class and initialized in onCreate but you forget to initialize counter on the OnCreate method which is why it is throwing a null pointer exception.
this.counter = (TextView) findViewById(R.id.txtNumber);
just initialize your counter variable just like you did on your welcome variable.
The problem was, that I tried to set the text of a TextView with an Integer
The fix was:
String numberString = Integer.toString(number);
counter.setText(numberString);

Generate random int and save it to SharedPreferences

I need to generate random int to give device id when my app run for the first time, then save it to SharedPreffs, show id on TextView, and when is turned on again show saved id from SharedPreffs on TextView.
public class MainActivity extends AppCompatActivity {
public static final String MyPREFERENCES = "MyPrefs";
public static final String KEY_DEVICE = "id";
SharedPreferences sharedpreferences;
String id;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
deviceid = (TextView) findViewById(R.id.deviceid);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
if (!sharedpreferences.contains(id)) {
SharedPreferences.Editor editor = sharedpreferences.edit();
id = String.valueOf(new Random().nextInt(900000) + 100000);
editor.putString(KEY_DEVICE, id);
editor.commit();
deviceid.setText(id);
}
deviceid.setText(id);
}
}
Above code generates random int and show it on TexView, but every time I hide or turn off the app, the device id changes
Could you explain me what i have to do to achive my goal.
There should be "id" or KEY_DEVICE
Replace
!sharedpreferences.contains(id)
to
!sharedpreferences.contains(KEY_DEVICE)
Also
deviceid.setText(id);
will show null in that case
So you have to add before setText
id = sharedpreferences.getString(KEY_DEVICE,"0");
Try this
String MyPREFERENCES = "MyPrefs";
String KEY_DEVICE = "device_id";
SharedPreferences sharedpreferences;
SharedPreferences.Editor editor;
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
if (sharedpreferences.contains(KEY_DEVICE)) {
id = sharedpreferences.getString(KEY_DEVICE, "0");
deviceid.setText(id);
} else {
id = String.valueOf(new Random().nextInt(900000) + 100000);
editor = sharedpreferences.edit();
editor.putString(KEY_DEVICE, id);
editor.apply();
deviceid.setText(id);
}
This is because you are just checking if there is no shared preference value then select a random number and add it to device but you are not getting any value in the on create method i.e you are not getting any shared preference value that you stored before. Simply get your stored value and then check if the value is exist, if exists then set in textview otherwise call the condition check. Hope it Helps
Put a check for null before fetching the value from sharedPreferences.
If it is null : then save the id
else : fetch the older one only.
If this check : if(!sharedpreferences.contains(id)) is required then keep it nested under null check.

Clear Shared Preferences

I am trying to make it possible that you can save a high score and I also need the user to be able to reset/delete their high score. the TOAST works but the data doesn't get deleted.
public static final String PREFS_NAME = "MyPrefsFile";
static SharedPreferences settings;
static SharedPreferences.Editor editor;
// When 'back' button is pressed save the highscore to settings
editor = settings.edit();// Create a new editor
editor.putInt("highscore", HighScore); // Storing integer
editor.commit();
// When 'Show' button is pressed
public void showPreferences(View v) {
int highscore = GameActivity.settings.getInt("highscore", GameActivity.HighScore);
Toast.makeText( MainMenu.this, "Your Highscore is: " + highscore, Toast.LENGTH_LONG).show();
}
//When delete button is pressed
public void clearPreferences(View V) {
GameActivity.editor = GameActivity.settings.edit();// Create a new editor
GameActivity.editor.clear();
GameActivity.editor.commit();
Toast.makeText( MainMenu.this,"Highscore has been reset",Toast.LENGTH_LONG).show();
}
I believe you are just reading it wrong, use this
int highscore = GameActivity.settings.getInt("highscore", 0);
Note that second parameter is the default value, a value that is returned if the value by that key is not present in the settings.
You can try this:
settings = getSharedPreferences("MyPrefsFile", 0);
preferences.edit().remove("highscore").commit();
Or you can update the sharepreference by the value 0.
Use the below to clear shared preferences
settings.edit().clear().commit();
Or use the below to clear single value from preferences
settings.edit().remove("highscore").commit();

issue with unmute button java android

I'm creating a simple click counter android app, sound is played when a button is clicked and the count is also saved when leaving the count screen and then returning to it.
I have encountered a problem with the mute button. When I click it, it mutes the whole application rather than just that particular gui screen (activity).
First issue is that the mute button mutes the sound for the whole app, and I only need to mute for that activity.
Second issue is that when you click mute button and come out of the screen, then go back, then try to unmute - it does not unmute the sound.
Was thinking the resolution to this is that we take the mute button out of the SharedPreferences save instance state - if this is possible...
Here is my code so far, if you can guide me on how to achieve the above that would be great. Thanks.
public class wazeefa extends Activity {
//Count Button
TextView txtCount;
ImageView image;
Button btnCount;
Button wmute;
Button wreset;
public static int count = 0;
SharedPreferences app_preferences;
MediaPlayer mpButtonClick;
AudioManager audioManager;
public static boolean mutestatus = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// The activity is being created.
setContentView(R.layout.wazeefa);
audioManager =
(AudioManager) getSystemService(Context.AUDIO_SERVICE);
//SAVE COUNT
app_preferences = this.getSharedPreferences("myPrefscount", MODE_WORLD_READABLE);
count = app_preferences.getInt("count", 0);
txtCount = (TextView) findViewById(R.id.wcount);
txtCount.setText("This app has been started " + count + " times.");
image = (ImageView) findViewById(R.id.imageview);
txtCount = (TextView) findViewById(R.id.wcount);
txtCount.setText("This app has been started " + count + " times.");
//Button SOUND AND COUNT
mpButtonClick = MediaPlayer.create(this, R.raw.bubble);
//RESET Button
wreset = (Button) findViewById(R.id.wreset);
txtCount = (TextView) findViewById(R.id.wcount);
txtCount.setText(String.valueOf(count));
btnCount = (Button) findViewById(R.id.wclick);
wmute = (Button) findViewById(R.id.wmute);
btnCount.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
count++;
if (count > 50) count = 0;
image.setImageResource(R.drawable.duroodimage);
if (count > 0) image.setImageResource(R.drawable.duroodimage);
if (count > 9) image.setImageResource(R.drawable.zikrimage);
if (count > 39) image.setImageResource(R.drawable.duroodimage);
txtCount.setText(String.valueOf(count));
mpButtonClick.start();
}
});
wreset.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
count = 0;
image.setImageResource(R.drawable.duroodimage);
;
txtCount.setText("0");
SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("count", count);
editor.commit();
}
});
wmute.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (!mutestatus) {
mutestatus = true;
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);
Log.v("'test....", "" + mutestatus);
} else {
mutestatus = false;
audioManager.setMode(AudioManager.MODE_NORMAL);
audioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, false);
Log.v("'test....", "" + mutestatus);
}
}
});
}
#Override
protected void onPause() {
super.onPause();
// save count value here
SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("count", count);
editor.commit();
}
}
You are saving the preferences on the application level, make it Activity specific, i.e. Implement mute functionality for the activity not the application.
Edit
See your objective is some what to mute and unmute (loquent) the audio, Preferences can be saved in three ways.
1) Preferences can be retrieved only by a single activity.
2 )Preferences can be shared and retrieved among all activities within the application.
3)Preferences can be shared and retrieved through all applications on the device.
In your case, Saving Activity-level preferences:
SharedPreferences prefs=getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor=prefs.edit();
editor.putString("pref 1", "some text");
editor.commit();
We get a SharedPreferences object by calling getPreferences(int mode) method which takes an integer value as a parameter, the mode value can be one of the following:
Context.MODE_PRIVATE (0): a file creating mode that makes the created file only accessible by applications with the same user ID (access the file from the same application context, will desctribe later).
Context.MODE_WORLD_READABLE (1): file mode makes the file readable from other applications.
Context.MODE_WORLD_WRITEABLE (2): file mode allows other applications to write to the file.
Then we get an instance of SharedPreferences.Editor and write the preference value with editor.putString(String key, String value) method.
shared preferences allows you to insert preferences using the following methods:
editor.putBoolean(String key, boolean value).
editor.putFloat(String key,float value).
editor.putInt(String key, int value).
editor.putLong(String key, long value)
editor.putString(String key, String value)
Then we call edit.commit() to save the preferences to the file. commit returns a boolean indicating the result of saving, true if successful and false if failed.
Reading preferences values:
To read preferences values:
SharedPreferences prefs=getPreferences(Context.MODE_PRIVATE);
String val=prefs.getString("pref 1", "some text");
We use sharedpreferences.getString(String key, String defaultValue) (or get boolean/float/int) to return the value stored with a specific key or defaultValue if not found.
Source

Save The value of Variable in Java/Android [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Making data persistent in android
Edited:
I am building an android app which works on the principle of a simple counter.
public class TasbeehActivity extends Activity {
/** Called when the activity is first created. */
int count;
ImageButton imButton;
TextView display;
static String ref = "Myfile";
static String key = "key";
SharedPreferences myPrefs;
boolean check = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imButton = (ImageButton) findViewById(R.id.bCount);
display = (TextView) findViewById(R.id.tvDisplay);
myPrefs = getSharedPreferences(ref, 0);
if(check){
String dataReturned = myPrefs.getString(key, "0");
count = Integer.parseInt(dataReturned);
display.setText(""+count);
}
imButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Random rand = new Random();
display.setTextColor(Color.rgb(rand.nextInt(255),
rand.nextInt(255), rand.nextInt(255)));
count++;
display.setText("" + count);
}
});
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
String data = display.getText().toString(); SharedPreferences.Editor
editor = myPrefs.edit(); editor.putString(key, data);
editor.commit();
check = true;
}}
I want to save the value of count so that when my app restarts after closing it should have the value of count before closing app.
Also when I change orientation of the emulator i.e. from portrait to landscape or vice versa count gets set to 0.
Is there any solution for both of these problems?
You should use SharedPreferences to save your variable, and Override OnConfigurationChange because it's probably calling your oncreate method (don't forget to add android:configChanges="orientation|keyboardHidden|screenSize" in your manifest)
I Hope this will help you.
1. use sharedpreferences for saving or retrieving value of count when app is restarts after closing
2. see handle orientation change in android count gets set to 0 when I change orientation of the emulator i.e. from portrait to landscape or vice versa
Try these....
android:configChanges="orientation" , This will prevent your Activity to again restart when orientation changes.
Use Bundle, but thats only for small amount of data. Serialization and Deserialization will be needed.
You can also use onRetainNonConfigurationInstance(), and getLastNonConfigurationInstance(), for storing and retrieving data.
See this link for further details:
http://developer.android.com/guide/topics/resources/runtime-changes.html

Categories

Resources