所以,我正在尝试保存一条我认为应该保存在共享首选项中的消息,这样我以后就可以编辑它并与其他片段共享。我有一个带有按钮的片段,打开一个包含编辑框的对话框,我想将任何写入编辑框的内容保存为一个字符串,以后可以在另一个活动中使用。问题是,一旦设置好后我就无法收回消息,所以我根本无法编辑消息。
这是按钮点击,它设置了要放入选项的对话框,包括一个广播组和编辑文本框:
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 :(得分:0)
在SharedPreference
中添加消息后,您必须提交消息。
在您的活动中尝试如下以优先添加值:
SharedPreferences prefs = getActivity().getSharedPreferences("my_health_settings", Activity.MODE_PRIVATE);
SharedPreferences.Editor mEditor = prefs.edit();
mEditor.putString("message", msg);
mEditor.commit();