片段和对话框之间的共享首选项

问题描述:

所以,我试图保存一条消息,我相信应该保存在共享首选项中,以便稍后可以对其进行编辑并与其他片段共享。我有一个带有一个按钮的片段,它打开一个包含编辑框的对话框,并且我希望将任何写入编辑框的内容保存为可供稍后在另一个活动中使用的字符串。问题在于,一旦设置完毕,我无法返回消息,因此我根本无法编辑消息。片段和对话框之间的共享首选项

这是按钮点击该设置对话框放于选项,包括单选按钮组和编辑文本框:

case R.id.settings_my_health_alert_settings: 
      //Set Up Dialog 
      final Dialog dialog = new Dialog(getActivity()); 
      dialog.setContentView(R.layout.myhealth_alert_settings); 
      dialog.setCancelable(false); 
      dialog.setTitle(R.string.health_alert_dialogue_title); 

      // set the custom dialog components - text, image and button 
      RadioButton1 = (RadioButton)dialog.findViewById(R.id.intervalOnce); 
      RadioButton2 = (RadioButton)dialog.findViewById(R.id.interval5min); 
      RadioButton3 = (RadioButton)dialog.findViewById(R.id.interval10min); 
      radioGroup = (RadioGroup) dialog.findViewById(R.id.radioGroup); 

      Button dialogButton = (Button) dialog.findViewById(R.id.ok_btn); 
      Button cancelButton = (Button) dialog.findViewById(R.id.cancel); 
      // if button is clicked, close the custom dialog 
      dialogButton.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        if(RadioButton1.isChecked()) { 
         AlertService.isOnce = true; 
        } 
        if(RadioButton2.isChecked()){ 
         AlertService.isOnce = false; 
         intervalTime = 5; 
        } 
        if(RadioButton3.isChecked()){ 
         AlertService.isOnce = false; 
         intervalTime = 10; 
        } 

        SharedPreferences.Editor mEditor = prefs.edit(); 
        mEditor.putString("message", msg); 
        mEditor.apply(); 

        dialog.dismiss(); 
       } 
      }); 

这是OnCreateView的一部分似乎是地方如果我想从共享首选项获取字符串以显示每次打开对话框的时间,请将其放入。但我一直得到一个空指针。

SharedPreferences prefs = getActivity().getSharedPreferences("my_health_settings", Activity.MODE_PRIVATE); 
        SharedPreferences.Editor mEditor = prefs.edit(); 
        mEditor.putString("message", msg); 
        mEditor.commit(); 

        String edt = prefs.getString("message", ""); 
        if (edt == null) { 
         editT = "Emergency at: " +MyHealthFragment.address; 
         editTextBox.setText(editT); 
        } else { 
         editTextBox.setText(edt); 
        } 

请记住,共享首选项中的字符串还需要可以被其他片段访问。

我找不出最好的逻辑 - 把它放在onCreateView?或onClick?或者我错过了一些完全不同的东西,然后,如果在编辑框中没有设置任何东西,就有一个标准的备份信息......任何帮助都会很棒。我希望这是有道理的。

在此先感谢。

编辑:这是我得到的logcat:

Caused by: java.lang.NullPointerException 
     at com.nesscorporation.android.sibext.fragment.settings.dropdown.MyHealthSettingsFragment.<init>(MyHealthSettingsFragment.java:58) 
     at com.nesscorporation.android.sibext.fragment.settings.dropdown.MyHealthSettingsFragment.getInstance(MyHealthSettingsFragment.java:41) 

我实际上没有一个警报设置片段,只是XML的对话。

这里是整个片段(有一些事情,我想出来):

public class MyHealthSettingsFragment extends BaseSettingsFragment implements View.OnClickListener { 

public static MyHealthSettingsFragment getInstance(){ 
    return new MyHealthSettingsFragment(); 
} 

private static final String TAG = "Koz"; 

private View pillRemindTitle; 
private View contactsTitle; 
private View alertSettings; 
public static String message = ""; 
public static int intervalTime = 5; 
public RadioButton RadioButton1; 
public RadioButton RadioButton2; 
public RadioButton RadioButton3; 
private RadioGroup radioGroup; 
private EditText editTextBox; 
private String editT; 
private String msg; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = View.inflate(getActivity(), R.layout.settings_my_health_fragment, null); 

    pillRemindTitle = v.findViewById(R.id.settings_my_health_pills_rem); 
    contactsTitle = v.findViewById(R.id.settings_my_health_contact_title); 
    alertSettings = v.findViewById(R.id.settings_my_health_alert_settings); 
    editTextBox = (EditText) v.findViewById(R.id.edit_message); 

    pillRemindTitle.setOnClickListener(this); 
    contactsTitle.setOnClickListener(this); 
    alertSettings.setOnClickListener(this); 

    return v; 
} 

@Override 
public void onClick(View v) { 
    switch (v.getId()){ 

     case R.id.settings_my_health_pills_rem: 
      startActivity(new Intent(getActivity(), PillRemindersActivity.class)); 
      break; 
     case R.id.settings_my_health_contact_title: 
      startActivity(new Intent(getActivity(), ContactsActivity.class)); 
      break; 
     case R.id.settings_my_health_alert_settings: 
      //Set Up Dialog 
      final Dialog dialog = new Dialog(getActivity()); 

      //final String msg = message.getText().toString(); 
      dialog.setContentView(R.layout.myhealth_alert_settings); 
      dialog.setCancelable(false); 
      dialog.setTitle(R.string.health_alert_dialogue_title); 
      ((TextView)dialog.findViewById(android.R.id.title)).setTypeface(Typeface.createFromAsset(getActivity().getAssets(),"walkway_expand_bold.ttf")); 

      // set the custom dialog components - text, image and button 
      RadioButton1 = (RadioButton)dialog.findViewById(R.id.intervalOnce); 
      RadioButton2 = (RadioButton)dialog.findViewById(R.id.interval5min); 
      RadioButton3 = (RadioButton)dialog.findViewById(R.id.interval10min); 
      radioGroup = (RadioGroup) dialog.findViewById(R.id.radioGroup); 

      Button dialogButton = (Button) dialog.findViewById(R.id.ok_btn); 
      Button cancelButton = (Button) dialog.findViewById(R.id.cancel); 
      // if button is clicked, close the custom dialog 
      dialogButton.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        if(RadioButton1.isChecked()) { 
         AlertService.isOnce = true; 
        } 
        if(RadioButton2.isChecked()){ 
         AlertService.isOnce = false; 
         intervalTime = 5; 
        } 
        if(RadioButton3.isChecked()){ 
         AlertService.isOnce = false; 
         intervalTime = 10; 
        } 

        SharedPreferences prefs = getActivity().getSharedPreferences("my_health_settings", Activity.MODE_PRIVATE); 
        SharedPreferences.Editor mEditor = prefs.edit(); 
        mEditor.putString("message", msg); 
        mEditor.commit(); 

        String edt = prefs.getString("message", ""); 
        //Log.d(TAG, edt); 
        if (edt == null) { 
         //editT = "Emergency at: " +MyHealthFragment.address; 
         //editTextBox.setText(editT); 
         Log.d(TAG, "null"); 
        } else { 
         Log.d(TAG, edt); 
         //editTextBox.setText(edt); 
        } 

        dialog.dismiss(); 
       } 
      }); 

      cancelButton.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        dialog.dismiss(); 
       } 
      }); 

      dialog.show(); 
      //SMS or MMS: radio group sms or mms (with image of map) 
      break; 
    } 
} 

我试图让写进对话框编辑框中任何文本每次来在对话框编辑以便可以根据用户需求进行编辑。

+0

哪里是你的logcat?发表它。 – 2015-01-21 04:49:03

+0

在settings_my_health_alert_settings中写入首选项的值? – 2015-01-21 04:51:53

+0

请发布一些你的片段的代码,你从你的偏好中获得价值。 – GrIsHu 2015-01-21 05:24:42

在您的SharedPreference中添加消息后,您必须提交。 如下尝试在你的活动,优先增加价值:

 SharedPreferences prefs = getActivity().getSharedPreferences("my_health_settings", Activity.MODE_PRIVATE); 
     SharedPreferences.Editor mEditor = prefs.edit(); 
     mEditor.putString("message", msg); 
     mEditor.commit(); 
+0

是的,我已经尝试过,但它似乎没有工作,或改变这个问题。我也再次编辑了这个问题。 – Kozbot 2015-01-21 05:29:28

+0

您是否尝试过调试您的代码? – GrIsHu 2015-01-21 06:16:35