Error: No view found for id androidx(Jetpack) Preference library
Asked Answered
U

2

10

I was following the instructions on docs to use Android Jetpack preference library, but I am getting the following error

java.lang.RuntimeException: Unable to start activity ComponentInfo{ ... activity.SettingsActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f090111 (... :id/settings_container) for fragment SettingsFragment
...
Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f090111 (... :id/settings_container) for fragment SettingsFragment 

as I think this usually happens when there is no container to mount the fragment, but in my case there is a container(settings_container).

My code looks like below

SettingsActivity

class SettingsActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.settings_container, SettingsFragment())
            .commit()
    }
}

SettingsFragment

class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {

    setPreferencesFromResource(R.xml.pref_main, rootKey)
}

activity_settings.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/settings_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".activities.SettingsActivity">
</FrameLayout>

pref_main.xml (shortened)

<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android">

 <PreferenceCategory
            app:key="data_sync_category"
            app:title="@string/pref_data_sync_category">

        <SwitchPreferenceCompat app:key="data_sync_status"
                                app:title="@string/pref_data_sync_status_title"
                                app:summary="@string/pref_data_sync_status_summary"
                                android:defaultValue="true"
        />
        <ListPreference app:key="what_data_sync"
                        app:title="@string/pref_sync_frequency_title"
                        app:summary="@string/pref_sync_frequency_summary"
                        android:entries="@array/pref_sync_frequency_titles"
                        android:entryValues="@array/pref_sync_frequency_values"
                        android:defaultValue="30"
                        android:negativeButtonText="@null"
                        android:positiveButtonText="@null"
        />
    </PreferenceCategory>
</androidx.preference.PreferenceScreen>

As you can see code is almost identical to the code in documentation. I tried invalidate and restart, did not work.

Please help

Unnerve answered 25/1, 2019 at 13:57 Comment(0)
C
14

No, there isn't a container.

You never use setContentView() in your Activity, so that layout doesn't exist. tools:context is an IDE-only feature.

You need to use

setContentView(R.layout.activity_settings)

right after calling super.onCreate(savedInstanceState).

Covarrubias answered 25/1, 2019 at 14:0 Comment(3)
thanks it works now. As you can see in the docs there is no setContentView, so example is incomplete.Unnerve
may be they remove trivial parts from the code snippets, in order to simplify things.Unnerve
It's not trivial as it looks then :)Retainer
G
2
java.lang.RuntimeException: Unable to start activity ComponentInfo{ ... activity.SettingsActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f090111 (... :id/settings_container) for fragment SettingsFragment
...
Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f090111 (... :id/settings_container) for fragment SettingsFragment

according your code you did't set layout for activity

you are giving reference of settings_container but its not found in activity layout because they dont have layout file so you need to setContentView for your activity


setContentView(R.layout.name)

public void setContentView (int layoutResID)

Set the activity content from a layout resource. The resource will be inflated, adding all top-level views to the activity.


Replace your activity kt file with

class SettingsActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_settings)
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.settings_container, SettingsFragment())
            .commit()
    }
}
Gilbertson answered 25/1, 2019 at 14:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.