共享preferences讨论

共享preferences讨论

本文介绍了共享preferences讨论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习Android开发的萨姆斯Android的发展在24小时内的。我采取一个游戏叫theredone包含以下活动:

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 延伸 quizactivity ,而 quizactivity 扩展活动

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

我在执行共享preferences 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 -----------------------

你能不能帮我请,我不认为它是一个愚蠢的QS

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

推荐答案

您可以使用下面的code为共享preference参考::::而在你的code你没有onCreate方法

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();

    }
}

这篇关于共享preferences讨论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 01:15