title: java.lang.ClassCastException: androidx.preference.PreferenceScreen cannot be cast to android.preference.GenericInflater$Parent
body: <p>The app crashes immediately on launch and I cannot really work out why. Ive read online, that this error can occur when I dont use the androidx libaries everywhere. I havent found an old one so here I am. I dont know what exactly is the problem. I first had this problem after my pc froze while I was working on the app. Before it compiled just fine and everything worked as intended, after the crash I had to reset the emulator and delete a couple of R.java files, which belonged to a custom libary for app intros of Github.</p>
  <p><strong>Logcat</strong></p>
  <pre><code>    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.computertycoon/com.example.computertycoon.MainActivity}: java.lang.ClassCastException: androidx.preference.PreferenceScreen cannot be cast to android.preference.GenericInflater$Parent
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
      at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
      at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
      at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
      at android.os.Handler.dispatchMessage(Handler.java:107)
      at android.os.Looper.loop(Looper.java:214)
      at android.app.ActivityThread.main(ActivityThread.java:7356)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
   Caused by: java.lang.ClassCastException: androidx.preference.PreferenceScreen cannot be cast to android.preference.GenericInflater$Parent
      at android.preference.GenericInflater.inflate(GenericInflater.java:328)
      at android.preference.GenericInflater.inflate(GenericInflater.java:271)
      at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:343)
      at android.preference.PreferenceManager.setDefaultValues(PreferenceManager.java:670)
      at android.preference.PreferenceManager.setDefaultValues(PreferenceManager.java:629)
      at com.example.computertycoon.MainActivity.onCreate(MainActivity.java:244)
      at android.app.Activity.performCreate(Activity.java:7802)
      at android.app.Activity.performCreate(Activity.java:7791)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
  </code></pre>
  <p><strong>SettingsActivity</strong></p>
  <pre><code>package com.example.computertycoon;
  
  import android.os.Bundle;
  import androidx.appcompat.app.ActionBar;
  import androidx.appcompat.app.AppCompatActivity;
  import androidx.appcompat.widget.Toolbar;
  import android.view.MenuItem;
  
  public class SettingsActivity extends AppCompatActivity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      // View-Elemente aus XML-Layout Datei erzeugen lassen
      setContentView(R.layout.activity_settings);
  
      // Initialisieren der App Bar und Aktivieren des Up-Buttons
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_settings_activity);
      setSupportActionBar(toolbar);
      ActionBar actionBar = getSupportActionBar();
      actionBar.setDisplayHomeAsUpEnabled(true);
      actionBar.setTitle(getString(R.string.action_settings));
  
  
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      if (item.getItemId() == android.R.id.home) {
          onBackPressed();
          return true;
      }
      return super.onOptionsItemSelected(item);
  }
  
  }
  </code></pre>
  <p><strong>SettingsFragment</strong></p>
  <pre><code>package com.example.computertycoon;
  
  import android.os.Bundle;
  import android.util.Log;
  import androidx.preference.Preference;
  import androidx.preference.PreferenceFragmentCompat;
  
  
  public class SettingsFragment extends PreferenceFragmentCompat implements
      Preference.OnPreferenceChangeListener {
  
  @Override
  public void onCreatePreferences(Bundle bundle, String s) {
  
      addPreferencesFromResource(R.xml.preferences);
  
      Preference currency_list = findPreference(&quot;currency_list&quot;);
      currency_list.setOnPreferenceChangeListener(this);
  
      Preference switch_box_show_idle_dialog = findPreference(&quot;switch_show_idle_dialog&quot;);
      switch_box_show_idle_dialog.setOnPreferenceChangeListener(this);
  }
  
  @Override
  public boolean onPreferenceChange(Preference preference, Object newValue) {
  
      if (preference.getKey().equals(&quot;switch_show_idle_dialog&quot;)) {
          boolean switch_show_idle_dialog_Value = (boolean) newValue;
  
          Log.i(&quot;abc&quot;, &quot;toggled switch_show_idle_dialog, new value: &quot; + switch_show_idle_dialog_Value);
  
          return true;
      } else if (preference.getKey().equals(&quot;currency_list&quot;)) {
          String currency_list_Value = (String) newValue;
  
          Log.i(&quot;abc&quot;, &quot;changed currency, new currency: &quot; + currency_list_Value);
  
          return true;
      } else {
          return false;
  
      }
  }
  }
  </code></pre>
  <p><strong>Preferences.xml</strong></p>
  <pre><code>&lt;androidx.preference.PreferenceScreen xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;&gt;
  
  &lt;PreferenceCategory android:title=&quot;@string/settings_general&quot;&gt;
  
      &lt;ListPreference
          android:id=&quot;@+id/currency_list&quot;
          android:entries=&quot;@array/currency&quot;
          android:entryValues=&quot;@array/currency_values&quot;
          android:key=&quot;currency_list&quot;
          android:title=&quot;@string/settings_currency&quot;
          app:useSimpleSummaryProvider=&quot;true&quot; /&gt;
  
  &lt;/PreferenceCategory&gt;
  &lt;PreferenceCategory app:title=&quot;@string/settings_appearance&quot;&gt;
  
      &lt;SwitchPreference
          android:key=&quot;switch_show_idle_dialog&quot;
          android:summary=&quot;Get notified about your earnings which you got while being idle&quot;
          android:title=&quot;Show welcome dialog&quot;
          android:defaultValue=&quot;true&quot;
          app:defaultValue=&quot;true&quot; /&gt;
  
  
  &lt;/PreferenceCategory&gt;
  &lt;PreferenceCategory android:title=&quot;@string/settings_other&quot;&gt;
  
      &lt;Preference
          android:key=&quot;button_reset&quot;
          android:summary=&quot;@string/settings_repeat_intro_summary&quot;
          android:title=&quot;@string/settings_repeat_intro&quot;&gt;
          &lt;intent
              android:action=&quot;android.intent.action.MAIN&quot;
              android:targetClass=&quot;com.example.computertycoon.IntroActivity&quot;
              android:targetPackage=&quot;com.example.computertycoon&quot; /&gt;
      &lt;/Preference&gt;
  
  
  
      &lt;Preference
          android:key=&quot;button_reset&quot;
          android:summary=&quot;@string/settings_this_cant_be_undone&quot;
          android:title=&quot;@string/settings_reset_game&quot;
          app:icon=&quot;@drawable/ic_delete_forever_red_48dp&quot;&gt;
          &lt;intent
              android:action=&quot;android.intent.action.MAIN&quot;
              android:targetClass=&quot;com.example.computertycoon.ResetActivity&quot;
              android:targetPackage=&quot;com.example.computertycoon&quot; /&gt;
      &lt;/Preference&gt;
  
  &lt;/PreferenceCategory&gt;
  
  
  &lt;/androidx.preference.PreferenceScreen&gt;
  </code></pre>