android 有错误 "Attempt to invoke interface method on a null object reference".featFCM

android have error "Attempt to invoke interface method on a null object reference".featFCM

我有问题 我想控制按钮布尔值的 FCM 声音 我的布尔值是保存共享首选项 我做了 init 但为什么这个错误

这是我的按钮布尔值保存源

 private static SharedPreferences mPrefs;
        private static SharedPreferences.Editor mPrefsEditor;
        private static final String noti = "pref_key_noti_onoff";
     public static void initPrefs(Context context) {
            if( mPrefs == null){
                mPrefs = context.getSharedPreferences(context.getPackageName() + "_preferences", 0);
                mPrefsEditor = mPrefs.edit();
             }
        }
        public static boolean getSettingnoti() {
            return mPrefs.getBoolean(noti,true);
        }
  public static void setSettingnoti(boolean onoff) {
        mPrefsEditor = mPrefs.edit();
        mPrefsEditor.putBoolean(noti,onoff);
        mPrefsEditor.commit();

    }

这是我的 fcm 接收源

 public void onMessageReceived(RemoteMessage remoteMessage) {
            if (DataSaver.getSettingnoti()){}
            else{
                mPrefsEditor = mPrefs.edit();
                mPrefsEditor.putBoolean(noti,true);
                mPrefsEditor.commit();
            }

            if (DataSaver.getSettingnoti() == false) {
                Log.d(TAG, "onMessageReceived: ");
            } else {
                Integer.parseInt(DataSaver.getSettingsttime());
                Integer.parseInt(DataSaver.getSettingedtime());

                Log.d(TAG, "onMessageReceived:" + Integer.parseInt(DataSaver.getSettingsttime()) + Integer.parseInt(DataSaver.getSettingedtime()));

                sendPushNotification(remoteMessage.getData().get("message"));
                set_alarm_badge();
            }
        }

您需要修改此代码并在使用前对对象进行空检查。

public static boolean getSettingnoti() {
            if(mPrefs != null)
                return mPrefs.getBoolean(noti,true);
            /* put some log here */
            return false;
        }