我无法识别我的代码的问题,而且似乎没有人面临这种问题,因此无法在堆栈溢出中跟踪此问题。
异常消息:
Caused by: java.lang.ClassCastException: android.preference.CheckBoxPreference cannot be cast to android.preference.SwitchPreference
代码:
private Preference preference;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
addPreferencesFromResource(R.xml.settings);
loadAllViews();
}
private void loadAllViews()
{
if (VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH)
{
preference = (SwitchPreference) findPreference("preference"); //Exception occurs here.
}
else
{
preference = (CheckBoxPreference) findPreference("preference");
}
}
__________________ __________________ __________________
设置.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:key="preference"
android:title="yes_or_no" />
</PreferenceScreen>
有人请帮我弄清楚。