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