关于单身人士的问题

时间:2017-03-20 10:04:53

标签: android singleton

我参考了http://www.androidhive.info/2016/02/android-push-notifications-using-gcm-php-mysql-realtime-chat-app-part-2/ 我发现如果我使用代码,我就无法显示通知

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

它会显示错误MyApplication.getPrefManager()' on a null object reference

这是我的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;
    }
}

我该如何解决这个问题?任何帮助将不胜感激。

这是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 个答案:

答案 0 :(得分:1)

如果仅用于获取共享首选项实例,那么您实际上并不需要使Application类单例。如果要在共享首选项中应用单例模式,请在共享首选项相关类而不是Application类上执行此操作。

从它的外观来看,您使用MyPreferenceManager类来包含所有与共享首选项相关的代码。所以,我要说的是,改为单身。

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

}

并像这样称呼它

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

context中,根据活动或片段传递适当的上下文。