2016-08-05 42 views
4

我得到这个例外,只能从华为的设备采用Android 5.0.1:RuntimeException的内容有鉴于与id属性“android.R.id.list_container”这是不是一个ViewGroup类

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 /目标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搜索这里)似乎list_container SDK 24的一部分。无论如何,我无法理解的原因有关,因为我使用的版本至少与v14兼容,不幸的是我没有任何华为设备做一些尝试。

回答

4

如果您使用的是自定义布局,设置你使用(FrameLayout里,等),以android:id="@android:id/list_container"任何的ViewGroup的id

Android Studio中警告我说,这个属性需要API等级24,但我把它与API级19的设备上,它似乎工作

+0

错过setContentView我不使用自定义布局,我膨胀偏好头,然后每一个标题是PreferenceScreen。 – MoreOver

0

U ON

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

} 
+0

为什么?我没有使用自定义布局。 – MoreOver

相关问题