[英]why is onResume crashing my app on start up?
我正在尝试使用SharedPreferences保存复选框的状态。 我以为我的代码正确,因为它没有显示任何错误。 我检查了LogCat的原因,显然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>
编辑2
这是我最初从此处获取SharedPreferences代码的地方。 用户退出应用程序时如何保存Android CheckBox的状态?
该复选框为空,因为未分配给任何视图。 在您的oncreate视图中尝试。 确保在名为lBox1的xml文件中有一个复选框:
lBox1 = view.findViewById(R.id.check);//be sure to reference correct layout or you will get class cast exception
这是因为未定义CheckBox lBox1
。 在您的onCreate()
添加以下行:
lBox1 = (CheckBox) findViewById(R.id.check);
这应该可以解决您的问题。
onResume并不代表您的意思。
在生命周期开始时也会调用它。 它唯一恢复的是主UI线程。
因此,它在第一次运行应用程序之前先调用load()方法,然后再调用save(...)方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.