0

I take a reference with http://www.androidhive.info/2016/02/android-push-notifications-using-gcm-php-mysql-realtime-chat-app-part-2/ I find that if i use the code , i can't show the notification

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

it will show error MyApplication.getPrefManager()' on a null object reference

It's my MyApplication.class:

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;
    }
}

How do i fix the issue ? Any help would be appreciated .

Here is 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();
    }
}
Morton
  • 5,380
  • 18
  • 63
  • 118
  • 1
    It's a wrong place to implement the getPrefrerenceManager. This singleton pattern should be applied in MyPreferenceManager class instead. – fluffyBatman Mar 20 '17 at 10:07
  • 1
    Looking from this only code seems you missed passing context , `getPrefManager(Context mContext)` and then use it in line `pref = new MyPreferenceManager(mContext);` – MKJParekh Mar 20 '17 at 10:08
  • @MKJParekh How do i set context in this line `String oldNotification=MyApplication.getInstance().getPrefManager().getNotifications();` ? They are in my NotificationUtils class extends nothing – Morton Mar 20 '17 at 10:18
  • 1
    Check this http://stackoverflow.com/a/35765226/3111083 . Change your getInstance method to this and check. Reference https://en.wikipedia.org/wiki/Singleton_pattern. – Sunil Sunny Mar 20 '17 at 10:25
  • @sunilsunny , thanks for your information ,i had try it , it still shows the same error. – Morton Mar 20 '17 at 10:31
  • @徐博俊, can you please post MyPreferenceManager class? Here singleton class in not implemented properly. – Geek Mar 20 '17 at 10:34
  • sure , but i fix the issue from others suggestion , thanks all of you guys , i just focused on Context , the problem would be solved. – Morton Mar 20 '17 at 10:41

1 Answers1

1

You actually don't need to make the Application class singleton if it only for getting a shared preference instance. If you want to get a singleton pattern applied in your shared preference, do this on the shared preference related class instead of the Application class.

From the look of it, you're using MyPreferenceManager class to contain all shared preference related codes. So, I'd say, make it singleton instead.

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
    */

}

And call it like this

//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, pass your appropriate context depending on Activity or Fragment.

fluffyBatman
  • 6,524
  • 3
  • 24
  • 25