我想使用SharedPreferences保存我的複選框的狀態。我以爲我的代碼是正確的,因爲它沒有顯示任何錯誤。我檢查了LogCat的原因,顯然onResume有問題。爲什麼onResume在啓動時崩潰我的應用程序?
SuikodenFragment.java
public class SuikodenFragment extends Fragment implements OnItemClickListener {
public static final String suikodenprefs = "SuikodenPrefs" ;
ListView listView;
ArrayAdapter<Model> adapter;
List<Model> list = new ArrayList<Model>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.suikoden_main_activity1, container, false);
listView = (ListView) view.findViewById(R.id.my_list);
adapter = new SuikodenListAdapter(getActivity(),getModel());
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
//CheckBox lBox1 = (CheckBox) view.findViewById(R.id.check);
return view;
}
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
TextView label = (TextView) v.getTag(R.id.label);
CheckBox checkbox = (CheckBox) v.getTag(R.id.check);
Toast.makeText(v.getContext(), label.getText().toString()+" "+isCheckedOrNot(checkbox), Toast.LENGTH_LONG).show();
}
private String isCheckedOrNot(CheckBox checkbox) {
if(checkbox.isChecked())
return "is checked";
else
return "is not checked";
}
private void save(final boolean isChecked) {
SharedPreferences settings = getActivity().getSharedPreferences(suikodenprefs, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("check", isChecked);
editor.commit();
}
private boolean load() {
SharedPreferences settings = getActivity().getSharedPreferences(suikodenprefs, 0);
return settings.getBoolean("check", false);
}
@Override
public void onPause() {
super.onPause();
save(mCheckBox.isChecked());
}
@Override
public void onResume() {
super.onResume();
mCheckBox.setChecked(load());
}
也有在這個片段,我上面放,因爲它是相當長的一個ListView。我嘗試刪除onResume,應用程序啓動,但當我點擊一個複選框,並移動到另一個片段(從導航抽屜)它崩潰。這兩種崩潰都有相同的共同點。
因爲撞車的onResume ...
mCheckBox.setChecked(load());
當的onResume被刪除,因爲的onPause的崩潰發生(下文具體線)片段之間切換時...
save(mCheckBox.isChecked());
什麼想法?謝謝!
編輯按要求 - 以下
suikoden_activity_main1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/my_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
suikoden_row_1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dip"
android:text="@+id/label"
android:textSize="25sp" >
</TextView>
<CheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false" >
</CheckBox>
EDIT 2
這是我從原來的拿到了SharedPreferences代碼。 How to save the state of an Android CheckBox when the users exits the application?
'lBox1'可能爲null – Raghunandan
請看'Activity'生命週期。 'onResume'在'onCreateView'之前被調用。發生'NullPointerException'是因爲lBox1沒有被初始化。 – Wakim
@Wakim恰恰相反 –