2014-06-17 261 views
0

我想使用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?

+0

'lBox1'可能为null – Raghunandan

+0

请看'Activity'生命周期。 'onResume'在'onCreateView'之前被调用。发生'NullPointerException'是因为lBox1没有被初始化。 – Wakim

+0

@Wakim恰恰相反 –

回答

0

该复选框为空,因为它未分配给任何视图。在你的创建视图中试试这个。请确保你在你的XML文件名为lBox1有一个复选框:

lBox1 = view.findViewById(R.id.check);//be sure to reference correct layout or you will get class cast exception 
+0

嘿,谢谢你的回答,我实施了该行,并出于某种原因,它说我不能从视图转换复选框 – theCreed

+0

@theCreed这个文件的内容R.layout.suikoden_main_activity1 –

+0

我已经更新了代码。我会说,lBox1来自我在某处找到的教程。它可以改变成任何东西,即时消息不会被榨取。谢谢! – theCreed

0

这是因为CheckBox lBox1是不确定的。 在你onCreate()添加以下行:

lBox1 = (CheckBox) findViewById(R.id.check); 

这应该解决您的问题。

+0

实现此代码,它是说,lBox1本地变量的值不使用 – theCreed

+0

您已将此添加到onCreate()?也看到我刚刚编辑的编辑。 –

0

onResume并不意味着你的想法。

它在生命周期开始时也被调用。它唯一重新开始的是主UI线程。

因此,它在第一次运行应用程序时调用load()方法之前调用save(...)方法。

+0

好的,谢谢,我该怎么改变它?我在onStart和onStop上交换了onResume和onPause ...并且它仍然在同一行上崩溃 – theCreed

+0

onPause()很好。不要改变它。当然不要使用onStop()。只要警惕onResume()被调用时不会创建sharedpreference。 –

+0

好的谢谢,我该如何防范呢?对不起,所有的问题,但即时通讯初学者,感谢您的耐心 – theCreed