Jump to content

Gérer SharedPreferences dans une class simple


Recommended Posts

Je me suis créé une classe pour gérer les préférences mais ça ne fonctionne pas, surement à cause de Activity

package android.helloworld;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Toast;

public class preferences extends Activity {
   public static final String PREFS_NAME = "account";
   public SharedPreferences settings;
   SharedPreferences.Editor editor;
   Context context;

   public preferences(Context context) {
       this.context = context;
       settings = getSharedPreferences(PREFS_NAME, 0);
   }

   private void edit() {
       editor = settings.edit();
   }

   private boolean save() {
       if(editor.commit()) {
           Toast.makeText(context, "Préfèrences enregistrées", Toast.LENGTH_SHORT).show();
           return true;
       }else {
           return false;
       }
   }

   public long get_acc_id() {
       return settings.getLong("acc_id", 0);
   }

   public boolean set_acc_id(long id) {
       edit();
       editor.putLong("acc_id", id);
       return save();
   }
}

Dans mon main, quand je créé une instance de preferences ça plante

private preferences prefs;

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       prefs = new preferences(this);
   }

Y-a-t-il une solution à mon problème ?

Link to comment
Share on other sites

Lorsque que tu postes ton code et dis qu'il y a une erreur, n'hésite pas à donner les logs de ton console d'erreurs. Ça permet de trouver plus facilement le problème ;)

Si je me rappele bien, tu ne peux pas créer des instances d'Activity et donc c'est normal que ta classe Preferences ne fonctionne pas. Je ne comprends pas pourquoi tu souhaites que "preferences" hérite de "Activity". Pourquoi ne fais-tu pas une classe "preference" qiui hérite de "Object" et prend comme constructeur un objet "SharedPreferences"?

Link to comment
Share on other sites

Mon LogCat:

04-26 07:56:44.750: ERROR/AndroidRuntime(2923): ERROR: thread attach failed
04-26 07:56:45.050: ERROR/AndroidRuntime(2931): ERROR: thread attach failed
04-26 07:56:47.610: ERROR/ActivityManager(80): fail to set top app changed!
04-26 07:56:52.930: ERROR/AndroidRuntime(2940): ERROR: thread attach failed
04-26 07:56:53.270: ERROR/AndroidRuntime(2948): ERROR: thread attach failed
04-26 07:56:53.370: ERROR/AndroidRuntime(2954): Uncaught handler: thread main exiting due to uncaught exception
04-26 07:56:53.380: ERROR/AndroidRuntime(2954): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.helloworld/android.helloworld.main}: java.lang.NullPointerException
04-26 07:56:53.380: ERROR/AndroidRuntime(2954):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
04-26 07:56:53.380: ERROR/AndroidRuntime(2954):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)
04-26 07:56:53.380: ERROR/AndroidRuntime(2954):     at android.app.ActivityThread.access$2200(ActivityThread.java:126)
04-26 07:56:53.380: ERROR/AndroidRuntime(2954):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
04-26 07:56:53.380: ERROR/AndroidRuntime(2954):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-26 07:56:53.380: ERROR/AndroidRuntime(2954):     at android.os.Looper.loop(Looper.java:123)
04-26 07:56:53.380: ERROR/AndroidRuntime(2954):     at android.app.ActivityThread.main(ActivityThread.java:4595)
04-26 07:56:53.380: ERROR/AndroidRuntime(2954):     at java.lang.reflect.Method.invokeNative(Native Method)
04-26 07:56:53.380: ERROR/AndroidRuntime(2954):     at java.lang.reflect.Method.invoke(Method.java:521)
04-26 07:56:53.380: ERROR/AndroidRuntime(2954):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-26 07:56:53.380: ERROR/AndroidRuntime(2954):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-26 07:56:53.380: ERROR/AndroidRuntime(2954):     at dalvik.system.NativeStart.main(Native Method)
04-26 07:56:53.380: ERROR/AndroidRuntime(2954): Caused by: java.lang.NullPointerException
04-26 07:56:53.380: ERROR/AndroidRuntime(2954):     at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146)
04-26 07:56:53.380: ERROR/AndroidRuntime(2954):     at android.helloworld.preferences.(preferences.java:16)
04-26 07:56:53.380: ERROR/AndroidRuntime(2954):     at android.helloworld.main.onCreate(portefeuille.java:26)
04-26 07:56:53.380: ERROR/AndroidRuntime(2954):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-26 07:56:53.380: ERROR/AndroidRuntime(2954):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
04-26 07:56:53.380: ERROR/AndroidRuntime(2954):     ... 11 more

Edited by Roromix
Link to comment
Share on other sites

public class preferences extends Object {
   public static final String PREFS_NAME = "account";
   public SharedPreferences settings;
   SharedPreferences.Editor editor;
   Context context;

public preferences(Context context, SharedPreferences prefs) {
 this.context = context;
 mSharedPreferences = prefs;
 settings = getSharedPreferences(PREFS_NAME, 0);
}
....

du coup quand je crée l'instance dans mon main

prefs = new preferences(this, ??);

Je met quoi comme valeur à la place de "??" ?

Et dans ma classe préférences, qu'est-ce que je met dans mon constructeur pour appeler getSharedPreferences() qui est souligné en rouge depuis que j'ai supprimé l'héritage à "Activity"

Je voudrais juste pouvoir lire et modifier une préférence depuis une activité X ou Y à partir d'une instance de ma classe "preferences" donnant accès aux préférences. Si quelqu'un réussi à le faire, qu'il me le fasse savoir.

Merci par avance

Edited by Roromix
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...