Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SharedPreferences.Editor can't get editor to work

 
Corey Applegate
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm in the process of learning Java and code as it works with Android. I come for a code back grown of VB and .net. That is where I'm comfortable. Any how I'm reading the book "Sams Teach Yourself Android Application Development in 24 Hours" I'm on chapter 6 and stuck on the preference editor. All the code word but the last lines perfEditor.put..... Maybe there is another part to the project that is not yet installed but looking at the Development sight and other places on the Web I can not find any reference to that. Can one of you look at the code and tip me on what I'm missing?
prefEditor.PNG
[Thumbnail for prefEditor.PNG]
image of code
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please copy and paste the actual code next time; it's hard to read in a screenshot, and it can't be searched. You can UseCodeTags to make code look good in these forums.

The code looks OK, except that you're missing a prefEditor.commit() - which means that any change would not be saved. Are you getting an error message? If so, which one?
 
Corey Applegate
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code with commit. Trouble is it dose not have the error check underlined. I have not even run the code because of these errors.
first error line 11 is in line "prefEditor.putString("UserName","JaneDoe");"
the "." after preEditor is underlined and hover over with mouse says "Syntax error on token(s), misplaced construct(s)"
Second error same line most likely after correcting first will go away but "("UserName","JaneDoe")" Hover over says "Syntax error on tokens, delete these tokens"

Now with the Commit line errors at "commit" and hover over says "Syntax error on token "commit", Identifier expected after this token"




If you down load the fist image you will see the red underlines. Thanks for tacking the time with me.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, seeing it like that makes it clearer - the code must be inside a method. Declarations (like the first 3 lines) can be outside of methods, but statements (like the last two) can only exist inside of methods - possibly onCreate if it should run when the activity is created.
 
Corey Applegate
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep that did it. Moved it to my QuizSplashActivity. The Book didn't have me do the example just red about it. So I tried to get it to work to get a better feel for what they are saying. And when I did this I figured for myself I would put it in a place that I though would be the most to end of the project. Seeing that QuizSplashActivity extends from QuizActivity. Maybe I don't know what that means.. No I don't any how thanks for your help

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic