2016-08-05 11 views
4

Ich erhalte diese Ausnahme nur von Huawei-Geräte mit Android 5.0.1:Runtime Inhalt hat Ansicht mit id-Attribut ‚android.R.id.list_container‘, die keine Klasse Viewgroup ist

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) 

die einzige was ich verändert hat, ist die Bibliothek 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' 

und die compileSdkVersion/Ziel api 24.

Dies ist die Klasse, um die Fehler auf diesen Geräten zu erzeugen:

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); 
     } 
    } 
} 

Und das ist 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; 
    } 
} 

und von dem, was ich verstehe, habe (hier auf SO Suche) es im Zusammenhang scheint Teil sdk list_container 24. Auf jeden Fall kann ich den Grund nicht verstehen , da ich die Version mindestens mit v14 rückkompatibel benutze und leider habe ich kein Huawei Gerät um es auszuprobieren.

Antwort

4

Wenn Sie ein eigenes Layout verwenden, stellen Sie die ID, was auch immer Viewgroup Sie verwenden (FrameLayout, usw.) zu android:id="@android:id/list_container"

Android Studio mich warnt, dass dieses Attribut 24 API-Ebene erfordert, aber ich habe es ich bin nicht ein eigenes Layout verwenden, ich bin Aufpumpen präferenz Header auf einem Gerät mit API-Ebene 19, und es scheint

+0

verpassen zu arbeiten und dann jedem Header ist ein PreferenceScreen. – MoreOver

0

U setContentView auf

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

} 
+0

Warum? Ich verwende kein benutzerdefiniertes Layout. – MoreOver