androidx.preference.SwitchPreferenceCompat 无法转换为 androidx.preference.PreferenceGroup
所以我试图在我的 Android 应用程序中创建一个选项菜单,但是当我在模拟器中单击它时,应用程序崩溃并出现以下错误:
androidx.preference.SwitchPreferenceCompat cannot be cast to androidx.preference.PreferenceGroup
我真的不知道我在这里做错了什么,我希望有人知道。
设置活动:
public class SettingsActivity extends AppCompatActivity {
public static final String KEY_PREF_EXAMPLE_SWITCH = "example_switch";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportFragmentManager().beginTransaction()
.replace(android.R.id.content,
new SettingsFragment()).commit();
}
}
片段类
public class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState,
String rootKey) {
setPreferencesFromResource(R.xml.preferences, rootKey);
}
}
我的 MainActivity 中的
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
// Launch SettingsActivity
if (id == R.id.action_settings) {
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
方法:编辑 这也是我的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<SwitchPreferenceCompat xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreference
android:defaultValue="false"
android:key="vega_preference"
android:summaryOff="@string/vega_off_string"
android:summaryOn="@string/vega_on_string"
android:title="@string/vega_switch_title" />
<SwitchPreference
android:defaultValue="false"
android:key="vegan_preference"
android:summaryOff="@string/vegan_off_string"
android:summaryOn="@string/vegan_on_string"
android:title="@string/vegan_switch_title" />
</SwitchPreferenceCompat>
So I'm trying to make an options menu in my android app but when I click on it in the emulator the app crashes and I get the following error:
androidx.preference.SwitchPreferenceCompat cannot be cast to androidx.preference.PreferenceGroup
I don't really know what I am doing wrong here, I hope somebody knows.
The settings activity:
public class SettingsActivity extends AppCompatActivity {
public static final String KEY_PREF_EXAMPLE_SWITCH = "example_switch";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportFragmentManager().beginTransaction()
.replace(android.R.id.content,
new SettingsFragment()).commit();
}
}
The fragment class
public class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState,
String rootKey) {
setPreferencesFromResource(R.xml.preferences, rootKey);
}
}
methods in my MainActivity:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
// Launch SettingsActivity
if (id == R.id.action_settings) {
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
EDIT
Here is also my layout file:
<?xml version="1.0" encoding="utf-8"?>
<SwitchPreferenceCompat xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreference
android:defaultValue="false"
android:key="vega_preference"
android:summaryOff="@string/vega_off_string"
android:summaryOn="@string/vega_on_string"
android:title="@string/vega_switch_title" />
<SwitchPreference
android:defaultValue="false"
android:key="vegan_preference"
android:summaryOff="@string/vegan_off_string"
android:summaryOn="@string/vegan_on_string"
android:title="@string/vegan_switch_title" />
</SwitchPreferenceCompat>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
片段
setPreferencesFromResource(R.xml.preferences, rootKey)
是引发该问题的原因,因为它期望PreferenceGroup
而不是首选项视图。其中SwitchPreferenceCompat
不是PreferenceGroup
。这是因为您使用了 SwitchPreferenceCompat 作为设置屏幕的根元素。相反,您需要使用从
PreferenceGroup
扩展的PreferenceScreen
。因此,要解决此问题,您需要确保首选项布局的根元素是
PreferenceScreen
The snippet
setPreferencesFromResource(R.xml.preferences, rootKey)
is what raises that as it expects aPreferenceGroup
instead of a preference view. WhereSwitchPreferenceCompat
is notPreferenceGroup
.That is because you used a
SwitchPreferenceCompat
as the root element of settings screen. Instead you need to use thePreferenceScreen
which extends fromPreferenceGroup
.So to fix this you need to make sure that the root element of your preference layout is
PreferenceScreen