簡體   English   中英

為什么onResume在啟動時崩潰了我的應用程序?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM