javaandroidandroidxpreferenceactivity

Android App Crashing When Opening Settings


I'm creating an android app for a class project and this is my first time writing any android code. Basically I have a main activity and a preference activity. I added a menu where users can access the settings and to test I set up a toast message to display when settings is clicked. I started to implement the settings activity after watching a few videos and reading a few articles, but now when I click on settings the app crashes and I can't figure out why. Please help me.

MainActivity.java

public class MainActivity extends AppCompatActivity {
    EditText numChunks;
    TextView appear;
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Start
        numChunks = (EditText) findViewById(R.id.chunksInput);
        appear = (TextView) findViewById(R.id.display);
        btn = (Button) findViewById(R.id.button);

        btn.setOnClickListener(v -> {
            int chunks = Integer.parseInt(numChunks.getText().toString());

            appear.setText("You chose " + chunks + " chunks");
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.focus_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.settings:
                //Toast.makeText(this, "Opening settings...", Toast.LENGTH_SHORT).show();
                openSettings();
                return true;
            default:
                return super.onOptionsItemSelected(item);

        }

    }

    public void openSettings() {
        Intent intent = new Intent(this, SettingsActivity.class);
        startActivity(intent);
    }
}

SettingsActivity.java

public class SettingsActivity extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);
    }

}

settings.xml

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

    <PreferenceCategory
        android:title="Break Time">

        <CheckBoxPreference
            android:title="Short Break Time"
            android:key="short"
            android:defaultValue="false" />

        <CheckBoxPreference
            android:title="Long Break Time"
            android:key="long"
            android:defaultValue="false" />

    </PreferenceCategory>

    <PreferenceCategory
        android:title="About">

    </PreferenceCategory>

</androidx.preferencePreferenceScreen>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.omicron.focus">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Focus">
        <activity android:name=".SettingsActivity"
            android:label="Settings"/>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

App Error Log

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.omicron.focus, PID: 14313
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.omicron.focus/com.omicron.focus.SettingsActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class androidx.preferencePreferenceScreen
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        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:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class androidx.preferencePreferenceScreen
        at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:451)
        at android.preference.GenericInflater.inflate(GenericInflater.java:325)
        at android.preference.GenericInflater.inflate(GenericInflater.java:271)
        at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:343)
        at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1541)
        at com.omicron.focus.SettingsActivity.onCreate(SettingsActivity.java:13)
        at android.app.Activity.performCreate(Activity.java:8000)
        at android.app.Activity.performCreate(Activity.java:7984)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        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:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
     Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.preferencePreferenceScreen" on path: DexPathList[[zip file "/data/app/~~jjvjGm-imz3PHV2zESqthg==/com.omicron.focus-3PZT9wHKp29rqZJeJbYAYQ==/base.apk"],nativeLibraryDirectories=[/data/app/~~jjvjGm-imz3PHV2zESqthg==/com.omicron.focus-3PZT9wHKp29rqZJeJbYAYQ==/lib/x86, /system/lib, /system_ext/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:207)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.preference.GenericInflater.createItem(GenericInflater.java:383)
        at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:439)
        at android.preference.GenericInflater.inflate(GenericInflater.java:325) 
        at android.preference.GenericInflater.inflate(GenericInflater.java:271) 
        at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:343) 
        at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1541) 
        at com.omicron.focus.SettingsActivity.onCreate(SettingsActivity.java:13) 
        at android.app.Activity.performCreate(Activity.java:8000) 
        at android.app.Activity.performCreate(Activity.java:7984) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        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:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

Solution

  • So if you take a look at your error logs you can see what is causing the issue:

     Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class androidx.preferencePreferenceScreen
    
     Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.preferencePreferenceScreen" on path: ......
    
    

    You need to change this line

    <androidx.preferencePreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    

    to whatever it was previously or to the dependency.

    Easiest way to figure that out is create a new activity and just copy and paste across, then delete the activity. When I've upgraded my gradle its completely fucked things like this before.