-2

I am studying Android development for Sams Android Development in 24 hours. I am implementing a game called theredone that contains the following activities:

quizactivity, splashactivity, gameactivity, menuactivity, settingsactivity, helpactivity. splashactivity extends quizactivity while quizactivity extends Activity.

I implemented SharedPreferences in quizactivity.java.

package com.androidbook.triviaquiz6;

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

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

    private SharedPreferences myPrefs; //Syntax error on token ";", { expected after this token -//----------------------------


   myPrefs = Actionactivity.this.getSharedPreferences("myPrefs", MODE_WORLD_WRITEABLE);
   SharedPreferences.Editor prefsEditor = myPrefs.edit(); 
   prefsEditor.putString("key name", "key valuse"); 
   prefsEditor.commit(); 


   //this one to read data  
    myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE); 

    myPrefs.getString("key name", "") ;
}//Syntax error, insert "}" to complete ClassBody -----------------------

could you help me out please i dont think its a dumb qs

Rayne
  • 2,620
  • 20
  • 28
Karan Hans
  • 261
  • 1
  • 3
  • 7
  • Very related: http://stackoverflow.com/questions/10243080/i-would-like-others-to-help-me-on-sharedpreferences; please please pick up a good Java book & tutorial, you're missing the basics. – Mat Apr 21 '12 at 10:06
  • Might be the number in your package name. Oh an the fact your missing the Android Activity lifecycle methods! – Blundell Apr 21 '12 at 10:14

2 Answers2

1

you can use the below code as reference for shared preference::::And in your code you dont have oncreate method

//Retrive value from SharedPreference
SharedPreferences preferences = getApplicationContext().getSharedPreferences(GAME_PREFERENCES , android.content.Context.MODE_WORLD_WRITEABLE);
String value =  preferences.getString(key, null);


//Save value in SharedPreference
SharedPreferences preferences = getApplicationContext().getSharedPreferences(GAME_PREFERENCES , android.content.Context.MODE_WORLD_WRITEABLE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value);
editor.commit();

Updated:::

package com.androidbook.triviaquiz6;

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

public class QuizActivity extends Activity {
    public static final String GAME_PREFERENCES = "GamePrefs";
    private SharedPreferences myPrefs;
    protected void onCreate(Bundle savedInstanceState) {
    ....

    //this one data set in SharedPreferences file

    myPrefs = Actionactivity.this.getSharedPreferences("myPrefs", MODE_WORLD_WRITEABLE);
    SharedPreferences.Editor prefsEditor = myPrefs.edit();
    prefsEditor.putString("ket name", "key valuse");
    prefsEditor.commit();

    }
}
Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
1

You are referencing the Actionactivity activity in the QuizActivity activity. Actionactivity is not instantiate yet and your are opening a getSharedPreferences using that.

getSharedPreferences method is of context method and for current activity you can refer the this instance to call this getSharedPreferences method.

If you don't wan't to use activity's context then you can use the application context by calling getApplicationContext().

For more information about preference you can refer this

Community
  • 1
  • 1
Dharmendra
  • 33,296
  • 22
  • 86
  • 129