在广泛试图找出为什么我的SharedPreferences总是空白或从未创建之后。我的结论是,因为字符串的编辑文本没有做任何事情,但我不知道它为什么不做任何事情。
这是代码
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
public class Friday extends Fragment{
private String GBand, BBand, ADV1Band, ADV2Band, CBand, FBand;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
View view = inflater.inflate(R.layout.friday, container, false);
TextView GValue = (TextView) view.findViewById(R.id.bandText111111);
GValue.setText( "G Band" );
EditText GEdit = (EditText) view.findViewById(R.id.editText111111);
GBand = GEdit.getText().toString();
TextView BValue = (TextView) view.findViewById(R.id.bandText222222);
BValue.setText( "B Band" );
EditText BEdit = (EditText) view.findViewById(R.id.editText222222);
BBand = BEdit.getText().toString();
TextView ADV1Value = (TextView) view.findViewById(R.id.bandText333333);
ADV1Value.setText( "Advisory 1" );
EditText ADV1Edit = (EditText) view.findViewById(R.id.editText333333);
ADV1Band = ADV1Edit.getText().toString();
TextView ADV2Value = (TextView) view.findViewById(R.id.bandText444444);
ADV2Value.setText( "Advisory 2" );
EditText ADV2Edit = (EditText) view.findViewById(R.id.editText444444);
ADV2Band = ADV2Edit.getText().toString();
TextView CValue = (TextView) view.findViewById(R.id.bandText555555);
CValue.setText( "C Band" );
EditText CEdit = (EditText) view.findViewById(R.id.editText555555);
CBand = CEdit.getText().toString();
TextView FValue = (TextView) view.findViewById(R.id.bandText666666);
FValue.setText( "F Band" );
EditText FEdit = (EditText) view.findViewById(R.id.editText666666);
FBand = FEdit.getText().toString();
return view;
}
public class EditTextWatcher implements TextWatcher {
private final TextView target;
private EditTextWatcher(TextView target) {
this.target = target;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
this.target.setText(s);
}
public void onCreateOptionsMenu(
Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.android_apply, menu);
}
}
public boolean onOptionsItemSelected(android.view.MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.apply:
SharedPreferences sharedPref = getActivity().getSharedPreferences("schedule",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("g_Friday", GBand);
editor.putString("b_Friday", BBand);
editor.putString("adv1_Friday", ADV1Band);
editor.putString("adv2_Friday", ADV2Band);
editor.putString("c_Friday", CBand);
editor.putString("f_Friday", FBand);
editor.commit();
Intent in = new Intent(getActivity(), MainActivity.class);
startActivity(in);
return true;
}
return true;
}
}
答案 0 :(得分:4)
仔细看看你在那里做了什么:
EditText
的值。默认情况下,EditText
应该为空然后。EditText
中检索值 - 您只是使用在开头检索的空值。附注:Java编码约定通常调用变量的小写标识符,而大写标识符仅用于类,接口等。对于经验丰富的Java开发人员,您的代码将有些难以理解。
要修复它,请遵循此模式(仅针对GBand显示):
private String...
成员并声明private EditText edtGBand;
onCreateView()
,包含edtGBand = (EditText) view.findViewById( R.id.editText1111111 );
onOptionsItemSelected
中,使用editor.putString( "g_Friday", edtGBand.getText().toString() );
答案 1 :(得分:2)
尝试使edittexts受到保护并添加“string”=“edittext”.getText()。toString();在afterTextChanged()
之内