Android 为什么onResume在启动时会使我的应用程序崩溃?
我正在尝试使用SharedReferences保存复选框的状态。我认为我的代码是正确的,因为它没有显示任何错误。我查看了日志以了解原因,很明显,onResume有问题 SuikodenFragment.javaAndroid 为什么onResume在启动时会使我的应用程序崩溃?,android,android-fragments,checkbox,sharedpreferences,Android,Android Fragments,Checkbox,Sharedpreferences,我正在尝试使用SharedReferences保存复选框的状态。我认为我的代码是正确的,因为它没有显示任何错误。我查看了日志以了解原因,很明显,onResume有问题 SuikodenFragment.java public class SuikodenFragment extends Fragment implements OnItemClickListener { public static final String suikodenprefs = "SuikodenPrefs" ; Lis
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());
}
删除onResume时,由于在片段之间切换时出现onPause(下面的特定行),会发生崩溃
save(mCheckBox.isChecked());
有什么想法吗?谢谢
按要求编辑-以下内容
suikoden\u activity\u 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" />
<?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>
suikoden_row_1.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" />
<?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
这就是我最初获得SharedReferences代码的地方 复选框为空,因为它未分配给任何视图。在oncreate视图中尝试此操作。确保xml文件中有一个名为lBox1的复选框:
lBox1 = view.findViewById(R.id.check);//be sure to reference correct layout or you will get class cast exception
这是因为未定义
复选框lBox1
。
在onCreate()
中添加以下行:
lBox1 = (CheckBox) findViewById(R.id.check);
这应该可以解决你的问题。onResume并不是你认为它的意思 它也会在生命周期开始时被调用。它唯一要恢复的是主UI线程
因此,它会在第一次运行应用程序时调用save(…)方法之前调用load()方法 查看
活动
生命周期onResume
在onCreateView
之前调用。出现NullPointerException
是因为lBox1
没有初始化。@Wakim这只是一个相反的错误,我应该在创建视图时编写onCreateView
,让我来纠正这个错误。谢谢您的建议。据我所知,您在引用要在适配器中使用的复选框时犯了一个错误,即在片段中使用。从片段中删除所有lBox1code@IllegalArgument我已经更新了代码。我最初是从另一个问题的答案中得出的。链接在我的第一篇文章中。mCheckBox以前是lBox1,我只是把它改了一下,看看能不能让它工作。现在它又回到了原来的形式,也许你能帮我吗?嘿,谢谢你的回答,我实现了这一行,出于某种原因,它说我不能从视图转换为视图checkbox@theCreed发布此文件R.layout.suikoden_main_activity1的内容我已更新代码。我会说lBox1来自我在某处找到的一个教程。它可以改变成任何东西,我不担心。谢谢实现了这段代码,它的意思是lBox1局部变量的值未被使用。您将其添加到onCreate()中了吗?还可以看到我刚做的编辑。好的,谢谢,我要把它改成什么?我确实用onResume和onPause交换了onStart和onStop…但它仍然在同一行上崩溃。您的onPause()很好。不要改变它。当然不要使用onStop()。在调用onResume()时,请注意不要创建您的SharedReference。好的,谢谢,我该如何防止这种情况?对不起,我是初学者,谢谢你的耐心