Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 432
user158 Avatar asked Jan 25 '19 13:01

user158


2 Answers

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).

like image 98
TheWanderer Avatar answered Nov 18 '22 00:11

TheWanderer


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()
    }
}
like image 44
Ashvin solanki Avatar answered Nov 18 '22 01:11

Ashvin solanki