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
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)
.
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()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With