Or Bitton
Or Bitton

Reputation: 74

Android - IllegalStateException: Content has view with id attribute 'android.R.id.list_container' that is not a ViewGroup class

migrated an old project to AndroidX recently, and i'm struggling with implementing settings fragment. Basically I tried to add a settings fragment as an example with some basic preferences. I even used the studio settings activity sample code but all of my attempts returns the same exception.

"IllegalStateException: Content has view with id attribute 'android.R.id.list_container' that is not a ViewGroup class"

I tried to create a new project with sample settings activity and it works. i'm using this prefs version (although I tried others) implementation "androidx.preference:preference:1.1.1"

Ill attach the code of it although its a sample

Activity+Frag

public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);
        if (savedInstanceState == null) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.settings, new SettingsFragment())
                    .commit();
        }
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    public static class SettingsFragment extends PreferenceFragmentCompat {
        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            setPreferencesFromResource(R.xml.root_preferences, rootKey);
        }
    }
}

Activity Layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/settings"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

StackTrace:

  java.lang.IllegalStateException: Content has view with id attribute 'android.R.id.list_container' that is not a ViewGroup class
        at androidx.preference.PreferenceFragmentCompat.onCreateView(PreferenceFragmentCompat.java:202)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3138)
        at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3072)
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:251)
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:502)
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1510)
        at android.app.Activity.performStart(Activity.java:8315)
        at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3701)
        at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
        at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2308)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7898)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

What am I missing here? I can't get rid of this error

Upvotes: 2

Views: 97

Answers (1)

aya salama
aya salama

Reputation: 972

I kept digging in Google ANdroid issue Tracker till I found this bug, It was matching my case.

The solution simply is;

  1. Search for file preference_list_fragment.xml, in your code base

  2. Rename it as it's overriding another xml file in android.preferences library!

Upvotes: 1

Related Questions