2015-01-21 60 views
-1

所以,我試圖保存一條消息,我相信應該保存在共享首選項中,以便稍後可以對其進行編輯並與其他片段共享。我有一個帶有一個按鈕的片段,它打開一個包含編輯框的對話框,並且我希望將任何寫入編輯框的內容保存爲可供稍後在另一個活動中使用的字符串。問題在於,一旦設置完畢,我無法返回消息,因此我根本無法編輯消息。片段和對話框之間的共享首選項

這是按鈕點擊該設置對話框放於選項,包括單選按鈕組和編輯文本框:

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

回答

0

在您的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