gpt4 book ai didi

java - 呈现期间引发的异常:android.support.design.widget.AppBarLayout无法强制转换为android.preference.Preference

转载 作者:太空宇宙 更新时间:2023-11-04 12:09:42 28 4
gpt4 key购买 nike

我正在尝试使用上面的工具栏为我的应用创建一个设置窗口,以输入“后退”按钮等。但是我总是这个错误。

错误
呈现期间引发的异常:android.support.design.widget.AppBarLayout无法强制转换为android.preference.Preference

preferredenze.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory>
<ListPreference android:key="colore"
android:summary="ciao"
android:title="About "
android:defaultValue="#000000"
android:dialogTitle="colore di sfondo" />
</PreferenceCategory>
</PreferenceScreen>


settings.xml



<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_height="wrap_content"
android:layout_width="match_parent">

<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:toolbarId="@+id/toolbar"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
app:contentScrim="?attr/colorPrimary">

<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="?attr/actionBarTheme"
android:minHeight="?attr/actionBarSize"
android:id="@+id/toolbar" />

</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:id="@+id/activity_tips"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.twisterandroid.Tips_5">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:weightSum="1">

<include layout="@xml/preferenze"/>

</LinearLayout>
</RelativeLayout>


</android.support.design.widget.CoordinatorLayout>

最佳答案

我也有这个问题,花了我一些时间来解决。
脚步:

-使用AppCompatPreferenceActivity进行“设置”活动来扩展。
here获取此类并将其作为Java类放入您的项目中。

-首选项片段:

public class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings_fragment);
}
}


-在“首选项/设置片段”布局中(“偏好设置”屏幕),创建一个 PreferenceCategory并将其放在最顶层,作为布局的第一个元素,并在其中插入工具栏作为布局:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:layout="@layout/settings_toolbar" />
<!--<rest of the elements below it here>-->
</PreferenceScreen>


-创建 settings_toolbar布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>


-最终在“设置活动”中为工具栏充气,这是 Settings/PreferencesActivity的代码:

public class SettingsActivity extends AppCompatPreferenceActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

getLayoutInflater().inflate(R.layout.settings_toolbar, (ViewGroup) findViewById(android.R.id.content));
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("Settings or Preferences, etc ... ");
setSupportActionBar(toolbar);


SettingsFragment settingsFragmnet= new SettingsFragment();
getFragmentManager().beginTransaction().replace(android.R.id.content, settingsFragmnet).commit();
}
}

关于java - 呈现期间引发的异常:android.support.design.widget.AppBarLayout无法强制转换为android.preference.Preference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39964507/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com