22

I'm slowly working through an Android learning book and was given the following code to assign user data:

package com.androidbook.triviaquiz;

import android.app.Activity;
import android.content.SharedPreferences;

public class QuizActivity extends Activity {
    public static final String GAME_PREFERENCES = "GamePrefs";
    SharedPreferences settings = getSharedPreferences(GAME_PREFERENCES, MODE_PRIVATE);
    SharedPreferences.Editor prefEditor = settings.edit();
    prefeditor.putString("UserName", "John Doe"); //**syntax error on tokens**
    prefEditor.putInt("UserAge", 22); //**syntax error on tokens**
    prefEditor.commit();
}

However, I get an error (lines indicated with comments) that underlines the period and says "misplaced construct" and also that underlines the arguments saying "delete these tokens". I have seen this done in other applications in the same format, I don't understand what is wrong.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
Gaege
  • 815
  • 2
  • 9
  • 24

3 Answers3

37

Edit: Of course! Those statements cannot be put directly into the class at that level and must be inside a method, something like this:

public class QuizActivity extends Activity {
    public static final String GAME_PREFERENCES = "GamePrefs";

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        SharedPreferences settings = getSharedPreferences(GAME_PREFERENCES, MODE_PRIVATE);
        SharedPreferences.Editor prefEditor = settings.edit();
        prefEditor.putString("UserName", "John Doe");
        prefEditor.putInt("UserAge", 22);
        prefEditor.putString("Gender", "Male");
        prefEditor.commit();
    }
}
Howard
  • 53
  • 2
  • 9
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • should be all, but I am wondering about the type of errors though. – mad Feb 11 '11 at 09:43
  • Isn't prefEditor just referring to the instance I define immediately before? In that case there's no reason the capitalization should he different than the instance I declared. Maybe I'm missing something.... EDIT: I just noticed the case was different in the two prefeditor calls. Both were supposed to be prefEditor. However this solves nothing. – Gaege Feb 11 '11 at 09:47
  • Strange; I don't see anything else wrong with your code. What IDE are you using? As Markus Drösser pointed out, these are strange error messages. – trojanfoe Feb 11 '11 at 09:59
  • I'm working in eclipse with android plugin. All the books I've been reading kind of push that for beginners so I gave it a go. – Gaege Feb 11 '11 at 10:01
  • I have edited my answer - it was a glaring error that I missed. – trojanfoe Feb 11 '11 at 10:02
  • Haha glaring error. I'm still trying to understand the structure of things. I wish it was a glaring error for me. The book made no mention of the overriding or placement in the onCreate method. I'm trying to do this while learning Java and my only history with programming is an introductory course in C about a year ago. Not fun when "learning materials" aren't clear. – Gaege Feb 11 '11 at 10:07
  • Yeah it can't be easy - keep at it as I think you are learning something very worthwhile and rewarding and using the best programming language there is (but then all programmers say that about their language - if they didn't then something is wrong). – trojanfoe Feb 11 '11 at 10:32
  • Thanks trojan. I'll see if I can keep up with it while taking a full semester of mechanical engineering classes haha. Thanks for the help as well. – Gaege Feb 11 '11 at 19:54
4

I think you may missed up OnCreate() method ,let be sure you should place the shared preference in your OnCreate() method... i just edited your code go through it

please go through the code...below

public class A extends Activity {
static SharedPreferences settings;
 public static final String PREFS_NAME = "YourPrefName";

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        settings = getSharedPreferences(PREFS_NAME, 0);
Log.v("UserName"," - "+settings.getString("username","android"));
SharedPreferences.Editor editor = settings.edit();          
            editor.putString("username","Change Android");          
            editor.commit();

Log.v("UserName after changed editing preference key value"," - "+settings.getString("username","android"));


}

}
Senthil Mg
  • 3,301
  • 4
  • 32
  • 49
  • The book hadn't really mentioned the placement within the hierarchy of where this code should be placed. I assumed the last object the book had referred to, but perhaps I was mistaken. – Gaege Feb 11 '11 at 09:55
0

SharedPreferences will work out side a onCreate() method as long as it has a context:

SharedPreferences settings = getAplicationContext().getSharedPreferences(GAME_PREFERENCES, MODE_PRIVATE);
Eliud
  • 61
  • 5