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

前端之家收集整理的这篇文章主要介绍了RuntimeException Content具有id属性’android.R.id.list_container’的视图,该视图不是ViewGroup类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我只是从安装了 Android 5.0.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
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2406)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2466)
       at android.app.ActivityThread.access$1200(ActivityThread.java:152)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:135)
       at android.app.ActivityThread.main(ActivityThread.java:5538)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
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.v14.preference.PreferenceFragment.onCreateView(PreferenceFragment.java:278)
       at android.app.Fragment.performCreateView(Fragment.java:2059)
       at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:899)
       at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1076)
       at android.app.BackStackRecord.run(BackStackRecord.java:833)
       at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1461)
       at android.app.Activity.performStart(Activity.java:6031)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2466)
       at android.app.ActivityThread.access$1200(ActivityThread.java:152)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:135)
       at android.app.ActivityThread.main(ActivityThread.java:5538)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

我唯一改变的是库版本:

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

和compileSdkVersion / target到api 24.

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

public class PreferencesActivity extends AppCompatPreferenceActivity
        implements SharedPreferences.OnSharedPreferenceChangeListener
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActionBarUtils.titleBarSetup(getSupportActionBar(),this);
    }

    @Override
    public void onBuildHeaders(List<android.preference.PreferenceActivity.Header> target) {
        loadHeadersFromResource(R.xml.preferences_header,target);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key) {

    }

    /**
     * Subclasses should override this method and verify that the given fragment is a valid type to be attached to this
     * activity. The default implementation returns <code>true</code> for apps built for
     * <code>android:targetSdkVersion</code> older than {@link android.os.Build.VERSION_CODES#KITKAT}. For later
     * versions,* it will throw an exception.
     *
     * @param fragmentName the class name of the Fragment about to be attached to this activity.
     * @return true if the fragment class name is valid for this Activity and false otherwise.
     */
    @Override
    protected boolean isValidFragment(String fragmentName) {
        if(NetPrefs.class.getName().equals(fragmentName) ||
                LookPrefs.class.getName().equals(fragmentName) ||
                NotifyPrefs.class.getName().equals(fragmentName) ||
                GeneralPrefs.class.getName().equals(fragmentName))
            return true;

        return false;
    }

    public static class NetPrefs extends PreferenceFragment {
        @Override
        public void onCreatePreferences(Bundle bundle,String s) {
            addPreferencesFromResource(R.xml.preferences_net);
        }
    }

    public static class LookPrefs extends PreferenceFragment {
        @Override
        public void onCreatePreferences(Bundle bundle,String s) {
            addPreferencesFromResource(R.xml.preferences_look_feel);
        }
    }

    public static class NotifyPrefs extends PreferenceFragment {
        @Override
        public void onCreatePreferences(Bundle bundle,String s) {
            addPreferencesFromResource(R.xml.preferences_notifies);
        }
    }

    public static class GeneralPrefs extends PreferenceFragment {
        @Override
        public void onCreatePreferences(Bundle bundle,String s) {
            addPreferencesFromResource(R.xml.preferences_general);
        }
    }
}

这是AppCompatPreferenceActivity:

/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License,Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,software
 * distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.Toolbar;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * A {@link android.preference.PreferenceActivity} which implements and proxies the necessary calls
 * to be used with AppCompat.
 *
 * This technique can be used with an {@link android.app.Activity} class,not just
 * {@link android.preference.PreferenceActivity}.
 */
public abstract class AppCompatPreferenceActivity extends PreferenceActivity {

    private AppCompatDelegate mDelegate;

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

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        getDelegate().onPostCreate(savedInstanceState);
    }

    public ActionBar getSupportActionBar() {
        return getDelegate().getSupportActionBar();
    }

    public void setSupportActionBar(@Nullable Toolbar toolbar) {
        getDelegate().setSupportActionBar(toolbar);
    }

    @Override
    public @NonNull MenuInflater getMenuInflater() {
        return getDelegate().getMenuInflater();
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        getDelegate().setContentView(layoutResID);
    }

    @Override
    public void setContentView(View view) {
        getDelegate().setContentView(view);
    }

    @Override
    public void setContentView(View view,ViewGroup.LayoutParams params) {
        getDelegate().setContentView(view,params);
    }

    @Override
    public void addContentView(View view,ViewGroup.LayoutParams params) {
        getDelegate().addContentView(view,params);
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
        getDelegate().onPostResume();
    }

    @Override
    protected void onTitleChanged(CharSequence title,int color) {
        super.onTitleChanged(title,color);
        getDelegate().setTitle(title);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        getDelegate().onConfigurationChanged(newConfig);
    }

    @Override
    protected void onStop() {
        super.onStop();
        getDelegate().onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getDelegate().onDestroy();
    }

    public void invalidateOptionsMenu() {
        getDelegate().invalidateOptionsMenu();
    }

    private AppCompatDelegate getDelegate() {
        if (mDelegate == null) {
            mDelegate = AppCompatDelegate.create(this,null);
        }
        return mDelegate;
    }
}

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

解决方法

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

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

猜你在找的Android相关文章