cmackie21 cmackie21 - 3 months ago 31
Android Question

Error when setting default values for settings/Inflating Class Resources

I am currently trying to add a settings activity to my Android application. I have opted for the SettingsActivity with a fragment extending PreferenceFragment as recommended by the official tutorial by Android.

I am trying to set up the toolbar at the top of the app, again following the tutorial by Android.

The tutorial recommends you set the default values of the settings in the main activity, which is what I have done.

I keep getting an error when I load up the application. I have commented out the line which causes the error and the application loads fine if I do that so I have narrowed it down to this line.

PreferenceManager.setDefaultValues(this, R.xml.preferences, false);


preferences.xml does exist, in /res/xml/.

I have also read tutorials on Android settings but they seem to do nothing different to myself. I have also looked at questions posted by others on here but the error they received appeared to be because they included graphics whilst mine does not.

Here is my preferences.xml code:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!--Checkbox for series plot options grouped in subscreen-->
<PreferenceScreen
android:key="series_option_key"
android:title="@string/checkbox_option_title"
android:persistent="false">
<ListPreference
android:key="list_option_key"
android:title="@string/checkbox_option_title"
android:entries="@array/series_options"
android:defaultValue="@string/series_default">
</ListPreference>
</PreferenceScreen>

</PreferenceScreen>
</resources>


Fragment which houses the settings page:

public class Settings_ActivityFragment extends PreferenceFragment {

public Settings_ActivityFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Load preferences from xml file
addPreferencesFromResource(R.xml.preferences);
return inflater.inflate(R.layout.fragment_settings_, container, false);
}
}


The activity where this fragment lives:

public class Settings_Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new Settings_ActivityFragment())
.commit();
}

}


The associated xml layout file associated with the settings fragment currently has nothing in it apart from specifying a relative layout, as I have not found anywhere where I am required to add stuff to this file.

And the one you have all been waiting for...the stacktrace!

08-09 14:31:05.989 2470-2470/cmackie.nationalprofitgraphgenerator E/AndroidRuntime: FATAL EXCEPTION: main
Process: cmackie.nationalprofitgraphgenerator, PID: 2470
java.lang.RuntimeException: Unable to start activity ComponentInfo{cmackie.nationalprofitgraphgenerator/cmackie.nationalprofitgraphgenerator.Option_Activity}: android.view.InflateException: Binary XML file line #2: Error inflating class resources
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class resources
at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:443)
at android.preference.GenericInflater.inflate(GenericInflater.java:318)
at android.preference.GenericInflater.inflate(GenericInflater.java:264)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:273)
at android.preference.PreferenceManager.setDefaultValues(PreferenceManager.java:486)
at android.preference.PreferenceManager.setDefaultValues(PreferenceManager.java:445)
at cmackie.nationalprofitgraphgenerator.Option_Activity.onCreate(Option_Activity.java:37)
at android.app.Activity.performCreate(Activity.java:6245)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5443) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.preference.resources" on path: DexPathList[[zip file "/data/app/cmackie.nationalprofitgraphgenerator-2/base.apk"],nativeLibraryDirectories=[/data/app/cmackie.nationalprofitgraphgenerator-2/lib/arm, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at android.preference.GenericInflater.createItem(GenericInflater.java:376)
at android.preference.GenericInflater.onCreateItem(GenericInflater.java:419)
at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:430)
at android.preference.GenericInflater.inflate(GenericInflater.java:318) 
at android.preference.GenericInflater.inflate(GenericInflater.java:264) 
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:273) 
at android.preference.PreferenceManager.setDefaultValues(PreferenceManager.java:486) 
at android.preference.PreferenceManager.setDefaultValues(PreferenceManager.java:445) 
at cmackie.nationalprofitgraphgenerator.Option_Activity.onCreate(Option_Activity.java:37) 
at android.app.Activity.performCreate(Activity.java:6245) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5443) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
Suppressed: java.lang.ClassNotFoundException: Didn't find class "android.preference.resources" on path: DexPathList[[dex file "/data/data/cmackie.nationalprofitgraphgenerator/files/instant-run/dex/slice-support-annotations-24.1.1_260a6af0dfc8036856ad6ba2b9d281e7118bf977-classes.dex", dex file "/data/data/cmackie.nationalprofitgraphgenerator/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/cmackie.nationalprofitgraphgenerator/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/cmackie.nationalprofitgraphgenerator/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/cmackie.nationalprofitgraphgenerator/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/cmackie.nationalprofitgraphgenerator/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/cmackie.nationalprofitgraphgenerator/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/cmackie.nationalprofitgraphgenerator/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/cmackie.nationalprofitgraphgenerator/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/cmackie.nationalprofitgraphgenerator/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/cmackie.nationalprofitgraphgenerator/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/cmackie.nationalprofitgraphgenerator/files/in


I am happy to post any other code as required.

Answer

As per example on official doc page of PreferenceScreen, you need to get rid of the <resources> tag from your preferences.xml screen layout. So, your layout should look like this:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <!--Checkbox for series plot options grouped in subscreen-->
    <PreferenceScreen
        android:key="series_option_key"
        android:title="@string/checkbox_option_title"
        android:persistent="false">
        <ListPreference
            android:key="list_option_key"
            android:title="@string/checkbox_option_title"
            android:entries="@array/series_options"
            android:defaultValue="@string/series_default">
        </ListPreference>
    </PreferenceScreen>

</PreferenceScreen>
Comments