我正在尝试创建一个扩展 AppCompatActivity 并在其中有两个片段的 Activity(一个在另一个之下 - 只需使用 LinearLayout)。我希望第一个片段从 support-v7 库中扩展 PreferenceFragmentCompat 类。
我遵循了 Google 关于 PreferenceFragmentCompat 的简短示例,如https://developer.android.com/reference/android/support/v7/preference/PreferenceFragmentCompat.html所示。
这是我当前的代码:
GroupDetailsActivity.java
public class GroupDetailsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_group_details);
GroupDetailsPrefFragment prefFragment = GroupDetailsPrefFragment.newInstance();
GroupDetailsMembersFragment membersFragment = GroupDetailsMembersFragment.newInstance();
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
.add(R.id.flPrefFragment, prefFragment, GroupDetailsPrefFragment.TAG)
.add(R.id.flMembersFragment, membersFragment, GroupDetailsMembersFragment.TAG)
.commit();
}
}
GroupDetailsPrefFragment .java - 有问题的片段
public class GroupDetailsPrefFragment extends PreferenceFragmentCompat {
public static final String TAG = "GroupDetailsPrefFragment";
@Override
public void onCreatePreferences(Bundle bundle, String s) {
setPreferencesFromResource(R.xml.group_details_preferences, s);
}
public static GroupDetailsPrefFragment newInstance() {
GroupDetailsPrefFragment fragment = new GroupDetailsPrefFragment();
return fragment;
}
}
GroupDetailsMembersFragment.java - 现在完全是空的..
public class GroupDetailsMembersFragment extends Fragment {
public static final String TAG = "GroupDetailsMembersFragment";
public static GroupDetailsMembersFragment newInstance() {
GroupDetailsMembersFragment fragment = new GroupDetailsMembersFragment();
return fragment;
}
}
activity_group_details.xml - 活动的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/flPrefFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@+id/flMembersFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
group_details_preferences.xml - 首选项 XML 文件
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:title="@string/remove_me"
android:key="@string/pref_key_settings_remove_me"/>
<Preference
android:title="@string/delete_group"
android:key="@string/pref_key_settings_delete_group"/>
</PreferenceScreen>
尝试编译和运行上面的代码导致我遇到一些错误,第一个是关于未设置的首选项主题。我快速浏览了互联网,发现您需要将以下行添加到您的活动主题中:<item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>
为此,我需要将 support-v14 库添加到 gradle!
尝试再次运行代码,导致我出现另一个错误,这就是我发布此问题的原因,到目前为止我还没有找到解决此问题的任何方法。这是崩溃日志:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cochat.android/com.cochat.android.ui.groups.details.GroupDetailsActivity}: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list_container' that is not a ViewGroup class
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list_container' that is not a ViewGroup class
at android.support.v7.preference.PreferenceFragmentCompat.onCreateView(PreferenceFragmentCompat.java:269)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2184)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1298)
at android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(FragmentManager.java:2323)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2136)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1998)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:181)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1189)
at android.app.Activity.performStart(Activity.java:5441)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
一直停留在这个问题上,尝试在 stackoverflow 或其他网站上查找不同的帖子,看到了一些解决方案,但由于某种原因,他们都没有解决我的问题。
编辑:
我的 gradle 文件包含以下内容:
compileSdkVersion 25
buildToolsVersion '25.0.2'
...
compile 'com.android.support:preference-v7:25.1.0'
compile 'com.android.support:preference-v14:25.1.0'
2017 年 1 月 2 日更新
我一直在查看源代码PreferenceFragmentCompat
并看到它正在尝试加载以下布局:R.layout.preference_list_fragment
. 在onCreateView()
类的方法中,它正在膨胀布局,并试图寻找 id android.R.id.list_container
。问题是布局中没有这样的 id。
这是来自的代码片段PreferenceFragmentCompat
:
final View view = themedInflater.inflate(mLayoutResId, container, false);
final View rawListContainer = view.findViewById(AndroidResources.ANDROID_R_LIST_CONTAINER);
if (!(rawListContainer instanceof ViewGroup)) {
throw new RuntimeException("Content has view with id attribute "
+ "'android.R.id.list_container' that is not a ViewGroup class");
}
尽管
private int mLayoutResId = R.layout.preference_list_fragment;
仍在寻找解决方案,谢谢!