2017-03-20 6 views
0

Ich nehme einen Verweis mit http://www.androidhive.info/2016/02/android-push-notifications-using-gcm-php-mysql-realtime-chat-app-part-2/ Ich finde, dass, wenn ich den Code verwenden, kann ich nicht die Meldung anzeigenEin Problem, über Singletons

if (Config.appendNotificationMessages) {//it's true 
      //store the notification in shared pref first 
      MyApplication.getInstance().getPrefManager().addNotification(message); 
      //get the notification from shared preferences 
      String oldNotification = MyApplication.getInstance().getPrefManager().getNotifications(); 

      List<String> messages = Arrays.asList(oldNotification.split("\\|"));//待看 

      for (int i = messages.size() - 1; i >= 0; i--) { 
       inboxStyle.addLine(messages.get(i)); 
       System.out.println("for"); 
      } 
     } else { 
      inboxStyle.addLine(message); 
     } 

es Fehler zeigen MyApplication.getPrefManager()' on a null object reference

Es ist mein MyApplication. Klasse:

public class MyApplication extends Application { 
    public static final String TAG = MyApplication.class.getSimpleName(); 

    private static MyApplication mInstance; 

    private MyPreferenceManager pref; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     mInstance = this; 
    } 

    public static synchronized MyApplication getInstance() { 
     return mInstance; 
    } 

    public MyPreferenceManager getPrefManager() { 
     if (pref == null) { 
      pref = new MyPreferenceManager(this); 
     } 
     return pref; 
    } 
} 

Wie behebe ich das Problem? Jede Hilfe wäre willkommen. Hier

ist MyPreferenceManager

public class MyPreferenceManager { 
    private String TAG = MyPreferenceManager.class.getSimpleName(); 

    //Shared Preference 
    SharedPreferences pref; 

    //Editor for shared preference 
    SharedPreferences.Editor editor; 

    //Context 
    Context context; 

    //Shared pref mode 
    int PRIVATE_MODE = 0; 

    //SharedPref file name 
    private static final String PREF_NAME = "androidhive_gcm"; 

    //All Shared Preferences key 
    private static final String KEY_USER_ID = "user_id"; 
    private static final String KEY_USER_NAME = "user_name"; 
    private static final String KEY_USER_EMAIL = "user_email"; 
    private static final String KEY_NOTIFICATIONS = "notifications"; 


    //Constructor 
    public MyPreferenceManager(Context context) { 
     this.context = context; 
     pref = context.getSharedPreferences(PREF_NAME, PRIVATE_MODE); 
     editor = pref.edit(); 
    } 

    public void addNotification(String notification) { 
     //get old notifications 
     String oldNotifications = getNotifications(); 

     if (oldNotifications != null) { 
      oldNotifications += "|" + notification; 
     } else { 
      oldNotifications = notification; 
     } 
     editor.putString(KEY_NOTIFICATIONS, oldNotifications); 
     editor.commit(); 
    } 

    public String getNotifications() { 
     return pref.getString(KEY_NOTIFICATIONS, null); 
    } 

    public void clear() { 
     editor.clear(); 
     editor.commit(); 
    } 
} 
+1

Es ist ein falscher Ort, um den getPrefrerenceManager zu implementieren. Dieses Singleton-Muster sollte stattdessen in der MyPreferenceManager-Klasse angewendet werden. – fluffyBatman

+1

Wenn Sie von diesem Code schauen, haben Sie den passierenden Kontext verpasst, 'getPrefManager (Context mContext)' und dann in der Zeile 'pref = new MyPreferenceManager (mContext) verwenden;' – MKJParekh

+0

@MKJParekh Wie setze ich den Kontext in diese Zeile 'String oldNotification = MyApplication.getInstance(). GetPrefManager(). GetNotifications(); '? Sie sind in meiner NotificationUtils Klasse erweitert nichts –

Antwort

1

Sie eigentlich nicht brauchen, um den Application-Klasse Singleton zu machen, wenn es nur für eine gemeinsame Vorliebe Instanz zu bekommen. Wenn Sie ein Singleton-Muster in Ihrer gemeinsamen Einstellung anwenden möchten, tun Sie dies in der Klasse für die gemeinsame Präferenz statt für die Klasse Application.

Vom Aussehen her verwenden Sie MyPreferenceManager Klasse, um alle gemeinsamen Präferenz Codes enthalten. Also würde ich sagen, mach stattdessen Singleton.

public class MyPreferenceManager { 
    //Applying Singleton 
    private static MyPreferenceManager pref; 

    private MyPreferenceManager(){} 

    public static MyPreferenceManager getPrefManager(Context context) { 
     if (pref == null) { 
      pref = new MyPreferenceManager(context); 
     } 
     return pref; 
    } 

    /* 
    * Implementation of addNotification, getNotification and other methods 
    */ 

} 

Und nennen Sie es wie diese

//store the notification in shared pref first 
MyPreferenceManager.getPrefManager(context).addNotification(message); 
//get the notification from shared preferences 
String oldNotification = MyPreferenceManager.getPrefManager(context).getNotifications(); 

In context, übergeben Sie Ihren entsprechenden Kontext je nach Aktivität oder Fragmente.

+0

danke Mann, ich bekomme es jetzt, danke für Ihre Hilfe –

+0

Kein Problem. Glücklich, um irgendeine Hilfe zu sein. – fluffyBatman