RuntimeException Content具有id属性’android.R.id.list_container’的视图,该视图不是ViewGroup类

前端之家收集整理的这篇文章主要介绍了RuntimeException Content具有id属性’android.R.id.list_container’的视图,该视图不是ViewGroup类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我只是从安装了 Android 5.0.1的华为设备获得此例外:
  1. Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{net.example.app/net.example.app.view.PreferencesActivity}: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list_container' that is not a ViewGroup class
  2. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2406)
  3. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2466)
  4. at android.app.ActivityThread.access$1200(ActivityThread.java:152)
  5. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
  6. at android.os.Handler.dispatchMessage(Handler.java:102)
  7. at android.os.Looper.loop(Looper.java:135)
  8. at android.app.ActivityThread.main(ActivityThread.java:5538)
  9. at java.lang.reflect.Method.invoke(Method.java)
  10. at java.lang.reflect.Method.invoke(Method.java:372)
  11. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
  12. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
  13. Caused by java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list_container' that is not a ViewGroup class
  14. at android.support.v14.preference.PreferenceFragment.onCreateView(PreferenceFragment.java:278)
  15. at android.app.Fragment.performCreateView(Fragment.java:2059)
  16. at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:899)
  17. at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1076)
  18. at android.app.BackStackRecord.run(BackStackRecord.java:833)
  19. at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1461)
  20. at android.app.Activity.performStart(Activity.java:6031)
  21. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
  22. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2466)
  23. at android.app.ActivityThread.access$1200(ActivityThread.java:152)
  24. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
  25. at android.os.Handler.dispatchMessage(Handler.java:102)
  26. at android.os.Looper.loop(Looper.java:135)
  27. at android.app.ActivityThread.main(ActivityThread.java:5538)
  28. at java.lang.reflect.Method.invoke(Method.java)
  29. at java.lang.reflect.Method.invoke(Method.java:372)
  30. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
  31. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

我唯一改变的是库版本:

  1. compile 'com.android.support:appcompat-v7:24.1.1'
  2. compile 'com.android.support:support-v13:24.1.1'
  3. compile 'com.android.support:preference-v14:24.1.1'

和compileSdkVersion / target到api 24.

这是在这些设备上生成错误的类:

  1. public class PreferencesActivity extends AppCompatPreferenceActivity
  2. implements SharedPreferences.OnSharedPreferenceChangeListener
  3. {
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7.  
  8. ActionBarUtils.titleBarSetup(getSupportActionBar(),this);
  9. }
  10.  
  11. @Override
  12. public void onBuildHeaders(List<android.preference.PreferenceActivity.Header> target) {
  13. loadHeadersFromResource(R.xml.preferences_header,target);
  14. }
  15.  
  16. @Override
  17. public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key) {
  18.  
  19. }
  20.  
  21. /**
  22. * Subclasses should override this method and verify that the given fragment is a valid type to be attached to this
  23. * activity. The default implementation returns <code>true</code> for apps built for
  24. * <code>android:targetSdkVersion</code> older than {@link android.os.Build.VERSION_CODES#KITKAT}. For later
  25. * versions,* it will throw an exception.
  26. *
  27. * @param fragmentName the class name of the Fragment about to be attached to this activity.
  28. * @return true if the fragment class name is valid for this Activity and false otherwise.
  29. */
  30. @Override
  31. protected boolean isValidFragment(String fragmentName) {
  32. if(NetPrefs.class.getName().equals(fragmentName) ||
  33. LookPrefs.class.getName().equals(fragmentName) ||
  34. NotifyPrefs.class.getName().equals(fragmentName) ||
  35. GeneralPrefs.class.getName().equals(fragmentName))
  36. return true;
  37.  
  38. return false;
  39. }
  40.  
  41. public static class NetPrefs extends PreferenceFragment {
  42. @Override
  43. public void onCreatePreferences(Bundle bundle,String s) {
  44. addPreferencesFromResource(R.xml.preferences_net);
  45. }
  46. }
  47.  
  48. public static class LookPrefs extends PreferenceFragment {
  49. @Override
  50. public void onCreatePreferences(Bundle bundle,String s) {
  51. addPreferencesFromResource(R.xml.preferences_look_feel);
  52. }
  53. }
  54.  
  55. public static class NotifyPrefs extends PreferenceFragment {
  56. @Override
  57. public void onCreatePreferences(Bundle bundle,String s) {
  58. addPreferencesFromResource(R.xml.preferences_notifies);
  59. }
  60. }
  61.  
  62. public static class GeneralPrefs extends PreferenceFragment {
  63. @Override
  64. public void onCreatePreferences(Bundle bundle,String s) {
  65. addPreferencesFromResource(R.xml.preferences_general);
  66. }
  67. }
  68. }

这是AppCompatPreferenceActivity:

  1. /*
  2. * Copyright (C) 2014 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License,Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing,software
  11. * distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15.  
  16. import android.content.res.Configuration;
  17. import android.os.Bundle;
  18. import android.preference.PreferenceActivity;
  19. import android.support.annotation.LayoutRes;
  20. import android.support.annotation.NonNull;
  21. import android.support.annotation.Nullable;
  22. import android.support.v7.app.ActionBar;
  23. import android.support.v7.app.AppCompatDelegate;
  24. import android.support.v7.widget.Toolbar;
  25. import android.view.MenuInflater;
  26. import android.view.View;
  27. import android.view.ViewGroup;
  28.  
  29. /**
  30. * A {@link android.preference.PreferenceActivity} which implements and proxies the necessary calls
  31. * to be used with AppCompat.
  32. *
  33. * This technique can be used with an {@link android.app.Activity} class,not just
  34. * {@link android.preference.PreferenceActivity}.
  35. */
  36. public abstract class AppCompatPreferenceActivity extends PreferenceActivity {
  37.  
  38. private AppCompatDelegate mDelegate;
  39.  
  40. @Override
  41. protected void onCreate(Bundle savedInstanceState) {
  42. getDelegate().installViewFactory();
  43. getDelegate().onCreate(savedInstanceState);
  44. super.onCreate(savedInstanceState);
  45. }
  46.  
  47. @Override
  48. protected void onPostCreate(Bundle savedInstanceState) {
  49. super.onPostCreate(savedInstanceState);
  50. getDelegate().onPostCreate(savedInstanceState);
  51. }
  52.  
  53. public ActionBar getSupportActionBar() {
  54. return getDelegate().getSupportActionBar();
  55. }
  56.  
  57. public void setSupportActionBar(@Nullable Toolbar toolbar) {
  58. getDelegate().setSupportActionBar(toolbar);
  59. }
  60.  
  61. @Override
  62. public @NonNull MenuInflater getMenuInflater() {
  63. return getDelegate().getMenuInflater();
  64. }
  65.  
  66. @Override
  67. public void setContentView(@LayoutRes int layoutResID) {
  68. getDelegate().setContentView(layoutResID);
  69. }
  70.  
  71. @Override
  72. public void setContentView(View view) {
  73. getDelegate().setContentView(view);
  74. }
  75.  
  76. @Override
  77. public void setContentView(View view,ViewGroup.LayoutParams params) {
  78. getDelegate().setContentView(view,params);
  79. }
  80.  
  81. @Override
  82. public void addContentView(View view,ViewGroup.LayoutParams params) {
  83. getDelegate().addContentView(view,params);
  84. }
  85.  
  86. @Override
  87. protected void onPostResume() {
  88. super.onPostResume();
  89. getDelegate().onPostResume();
  90. }
  91.  
  92. @Override
  93. protected void onTitleChanged(CharSequence title,int color) {
  94. super.onTitleChanged(title,color);
  95. getDelegate().setTitle(title);
  96. }
  97.  
  98. @Override
  99. public void onConfigurationChanged(Configuration newConfig) {
  100. super.onConfigurationChanged(newConfig);
  101. getDelegate().onConfigurationChanged(newConfig);
  102. }
  103.  
  104. @Override
  105. protected void onStop() {
  106. super.onStop();
  107. getDelegate().onStop();
  108. }
  109.  
  110. @Override
  111. protected void onDestroy() {
  112. super.onDestroy();
  113. getDelegate().onDestroy();
  114. }
  115.  
  116. public void invalidateOptionsMenu() {
  117. getDelegate().invalidateOptionsMenu();
  118. }
  119.  
  120. private AppCompatDelegate getDelegate() {
  121. if (mDelegate == null) {
  122. mDelegate = AppCompatDelegate.create(this,null);
  123. }
  124. return mDelegate;
  125. }
  126. }

从我所理解的(在这里搜索SO)它似乎与sdk 24的list_container部分有关.无论如何我无法理解原因,因为我使用的版本至少与v14背面兼容,不幸的是我没有有任何华为设备做一些尝试.

解决方法

如果您正在使用自定义布局,请将您正在使用的任何ViewGroup的ID(FrameLayout等)设置为android:id =“@ android:id / list_container”

Android Studio警告我,此属性需要API级别24,但我将其放在API级别为19的设备上,似乎可以正常工作

猜你在找的Android相关文章