本文介绍了RuntimeException内容具有ID属性为"android.R.id.list_container"的视图,该视图不是ViewGroup类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仅从装有Android 5.0.1的华为设备获得此异常:

I'm getting this exception only from Huawei devices with 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)

我唯一更改的是库版本:

The only thing I changed is the library version:

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'

以及对api 24的compileSdkVersion/target.

and the compileSdkVersion/target to api 24.

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

This is the class generating the error on these devices:

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:

And this is 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版本兼容的版本,但不幸的是,没有任何华为设备可以尝试一下.

and from what I've understand (searching here on SO) it seems related to list_container part of sdk 24. Anyway I can't understand the reason, as I'm using the version backcompatible at least with v14 and unfortunately I don't have any Huawei device to do some try.

推荐答案

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

If you're using a custom layout, set the id of whatever ViewGroup you're using (FrameLayout, etc) to android:id="@android:id/list_container"

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

Android Studio warns me that this attribute requires API Level 24, but I put it on a device with API level 19 and it seems to work

这篇关于RuntimeException内容具有ID属性为"android.R.id.list_container"的视图,该视图不是ViewGroup类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 00:36