Bummer! You must be logged in to access this page.

Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Help with retaining variable data.

Hey guys! I know I have been posting a lot recently but I have very nearly finished my app and these posts are just to help me iron out the last few issues I am facing so sorry about my bombardment of posts!

Anyway, I am having some serious problems getting my app to retain the data of a double. This is the code I have so far that I have added to retain the data. What could be doing wrong?

@Override//This
    protected void onResume() {
        super.onStop();
        SharedPreferences settings = getSharedPreferences(moneyStorage, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(sCurMon, "");
        editor.commit();
 }

public static final String moneyStorage = "moneyStorageFile";

@Override
    protected void onStop() {
        super.onStop();
        SharedPreferences settings = getSharedPreferences(moneyStorage, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(sCurMon, "");
        editor.commit();


    }

    @Override//This
    protected void onPause() {
        super.onStop();
        SharedPreferences settings = getSharedPreferences(moneyStorage, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(sCurMon, "");
        editor.commit();


    }//To here
    @Override//This
    protected void onDestroy() {
        super.onStop();
        SharedPreferences settings = getSharedPreferences(moneyStorage, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(sCurMon, "");
        editor.commit();

    }//To here

The code doesn't actually give me any errors but when I click the home button on my phone and etc the variable doesn't retain its value.

Thank you to anyone who takes time out of their day to help me out.

-Luke

6 Answers

Add "static" back to the variable, but make sure to leave "final" out. Moreover, like Gunjeet said, move the variable declaration to the top of your class.

Lastly, I also recommend a good Java variable scope primer which could help explain some of the issues you are currently facing. Good luck!

Okay, I shall add static back. I already have moved it to the top of the class.

Hmm, I shall take a look online about it.

-Luke

You made moneyStorage a constant, by using the final keyword and hence why the value won't change.

Try:

public String moneyStorage = "moneyStorageFile";

Hi Miguel!

I tried changing it to what you said but now my app stops responding as soon as I press okay on my AlertDialog.

Well first I see you have declared a variable somewhere between two methods and also after onResume()

Technically moneyStorage will be undefined for onResume() since there is no existence of my moneyStorage. if exists only after the onReume method definition.

Try moving that right at the beginning of the class declaration as a member variable.

And yes as miguelcastro2 mentioned, since you declared it as final its become a constant.

Btw which variable are you referring to? Just so that I am on the right track

I tried doing what you said but now my app crashes when I click on the add money dialogs "ok" button.

Activity Life Cycle

So basically what I meant can be clear from the activity life cycle here. Your code is working fine. Great. But lets assume while someone is at your app and the phone rings, the activity goes into the pause state. When the phone disconnects the onResume activity will be called. Now since you defined the variable moneyStorage inside onResume before it is actually declared, it might just throw an error.

Also all of the methods as I see are trying to achieve a similar task. For all overridden methods you are calling the super classes onStop method. Is this on purpose? Can you tell me in brief the workflow of the application?

Thanks for the link Gunjeet!

I'm not so sure where onResume() and etc need to be positioned in my application. I took a rough guess after looking at the documentation and got no errors so just presumed that they would be fine as they are.

I moved moneyStorage above onResume() now so that should be fine.

Basically my program is a Money Managing application and the only value it needs to retain is the amount of money that is currently set in the TextView.

In that case you don't need static or the final keyword. Just move it right at the top and just before the setOnCreate method.

Do you intend to perform calculations with this received value?

Alright I have already removed final but I shall now go in and remove the static keyword also.

Not at this moment in time. All my application has to do at the moment is simply retain the value.

Ok. So now when you move it up to the top. The variable will hold the value from the input. But as soon as some other input is received it will replace it with that value.

So assuming that the user enters 300 in the textView. This value will be retained within moneyStorage throughout the length of the program. Now if you modify this value, this modified value will then be the set value within the variable. I hope this is what you intend to do.

That is what I intend to do yes.

However, now my app crashes after removing the "final static" keywords.

-Luke

Does it give an error?

Here it is:

 java.lang.NumberFormatException: Invalid double: ""
            at java.lang.StringToReal.invalidReal(StringToReal.java:63)
            at java.lang.StringToReal.parseDouble(StringToReal.java:248)
            at java.lang.Double.parseDouble(Double.java:295)
            at glazy.simplemoneymanager.MoneyManagerActivity$1$1.onClick(MoneyManagerActivity.java:62)
            at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4424)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)

This does not look like an error caused due to the removal of final and static. This is more like some part of your code is trying to parse a value with an empty string.

Did you initialise the moneyStorage variable? Also can you paste your code on line 63, 248 and 295 and any code related to that

I did indeed.

public String moneyStorage = "moneyStorageFile";

Line 63:

double amountToAdd = Double.parseDouble(input.getText().toString());

As for lines 248 and 295 I don't have anything on them. My app is only made up of 162 lines of code.

This seems fine, though it's difficult to tell simply looking at this one particular line.

If this project is not confidential why don't you put it up on GitHub or share it in some way so that I could get a better look and understanding.