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();
}
}
Es ist ein falscher Ort, um den getPrefrerenceManager zu implementieren. Dieses Singleton-Muster sollte stattdessen in der MyPreferenceManager-Klasse angewendet werden. – fluffyBatman
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
@MKJParekh Wie setze ich den Kontext in diese Zeile 'String oldNotification = MyApplication.getInstance(). GetPrefManager(). GetNotifications(); '? Sie sind in meiner NotificationUtils Klasse erweitert nichts –