非常感谢您在此寻找,我正在为学校开发应用程序而且我遇到了另一个问题。该应用程序的主要想法是跟踪你的卡路里,我希望它能够节省卡路里,所以当应用程序关闭时,它仍然会记住它们。我已经忙了一段时间现在尝试使用SavedPreferences,但我一直在收到错误。我希望有人能帮助我。
public class CaloriesEaten extends Fragment {
private Context mContext;
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private static EditText caleaten;
private static EditText calalready;
private static TextView caltotal;
private static Button btnClick;
private EditText editText;
//private String calalreadystring = calalready.getText().toString();
//int CalCount = Integer.parseInt(calalreadystring);
Context context = getActivity();
SharedPreferences pref = context.getSharedPreferences("MyCalories", Context.MODE_PRIVATE);
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public CaloriesEaten() {
// Required empty public constructor
}
public static CaloriesEaten newInstance(String param1, String param2) {
CaloriesEaten fragment = new CaloriesEaten();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
String keki = pref.getString("CalorieCounter", "");
editText.setText(keki);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_calories_eaten,
container, false);
caleaten = (EditText) view.findViewById(R.id.CalorieInput);
calalready = (EditText) view.findViewById(R.id.Cals);
caltotal = (TextView) view.findViewById(R.id.CalNumber);
btnClick = (Button)view.findViewById(R.id.addcalories);
btnClick.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonClicked(v);
}
});
return view;
}
public void buttonClicked (View view) {
int x = Integer.parseInt(caleaten.getText().toString());
int y = Integer.parseInt(calalready.getText().toString());
int total = x + y;
caltotal.setText(Integer.toString(total));
calalready.setText(Integer.toString(total));
caleaten.setText("");
SharedPreferences.Editor edit = pref.edit();
String calalreadystring = calalready.getText().toString();
int CalCount = Integer.parseInt(calalreadystring);
edit.putInt("CalorieCounter", CalCount);
edit.commit();
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
我可能做了很多明显的愚蠢的事情,但我真的无法理解。
非常感谢!
答案 0 :(得分:1)
全局声明SharedPreferences并在onCreateView方法中初始化
例如:
Context context;
SharedPreferences pref;
.......
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_calories_eaten,
container, false);
context = getActivity();
pref = context.getSharedPreferences("MyCalories", Context.MODE_PRIVATE);
.............
....
}