1

I'm still new in Android programming, and I just can't solve this problem. I'm creating a Preference Screen, but at the moment I run the application, I get the following error:

Error:Execution failed for task ':app:mergeDebugResources'.
> Unsupported type 'PreferenceScreen' in file C:\Users\Fidel Sebastián\AndroidStudioProjects\Asteroides\app\src\main\res\values\preferencias.xml

This is the XML file:

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:key="preferencias_principal" >
        <CheckBoxPreference
            android:key="music"
            android:title="Start music"
            android:summary="Select for background music"/>
        <ListPreference
            android:key="graphics"
            android:title="Graphics type"
            android:summary="Choose your graphics type"
            android:entries="@array/graphicstype"
            android:entryValues="@array/graphicsTypeValues"
            android:defaultValue="1"/>
        <EditTextPreference
            android:key="fragments"
            android:title="Number of fragments"
            android:summary="Select for meteor fragments"
            android:defaultValue="3"/>
    </PreferenceScreen>
</resources>

This is the Java file:

package com.example.fidelsebastin.asteroides;


import android.os.Bundle;
import android.preference.PreferenceFragment;

public class Preferences extends PreferenceFragment {
    @Override
   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.arrays);
    }
}

I've searched all over the Web, but I still can't find the answer. Any help would be really appreciated. Thanks in advance.

MexicanGuy
  • 13
  • 5

1 Answers1

5

Get rid of the <resource> tags. A PreferenceScreen is not a resource. It should be defined as the top-level object in the XML, as shown in the documentation example:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="preferencias_principal" >
    <CheckBoxPreference
        android:key="music"
        android:title="Start music"
        android:summary="Select for background music"/>
    <ListPreference
        android:key="graphics"
        android:title="Graphics type"
        android:summary="Choose your graphics type"
        android:entries="@array/graphicstype"
        android:entryValues="@array/graphicsTypeValues"
        android:defaultValue="1"/>
    <EditTextPreference
        android:key="fragments"
        android:title="Number of fragments"
        android:summary="Select for meteor fragments"
        android:defaultValue="3"/>
</PreferenceScreen>

Additionally:

  1. You need to move the preferences file into res/xml instead of res/values
  2. You should pass the identifier for the preferences file into addPreferencesFromResource(), not R.xml.arrays:

    public class Preferences extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferencias);
        }
    }
    
eldarerathis
  • 35,455
  • 10
  • 90
  • 93
  • 1
    Thanks, but now I have another error: "Element PreferenceScreen must be declared". What does that mean? – MexicanGuy Apr 18 '15 at 01:42
  • 1
    Move your preferences file out of `res/values` and into a `res/xml` folder. You also need to pass the right file into `addPreferencesFromResource()`, @MexicanGuy – eldarerathis Apr 18 '15 at 01:49
  • Many, many thanks man! I'm very grateful for you help. I just can't thank you enough! I know I'm going a little off-topic here, but final question: do you have any knowledge of a good YouTube channel that teaches Android programming? I'm more of the visual kind when it comes to learning. Again, thanks! – MexicanGuy Apr 18 '15 at 02:03
  • To be honest, I do not, @MexicanGuy. I've mostly used online texts and code samples from android.com. [CommonsWare](https://commonsware.com/) books are good resources (but not video-based). I've also heard generally good things about some of the University of Maryland's [Coursera classes](https://www.coursera.org/course/androidpart1), but haven't taken them myself. – eldarerathis Apr 18 '15 at 02:08
  • Oh I see, no problem-o. I'll check both the books and the classes later. Again, many thanks eldarerathis, and goodbye! – MexicanGuy Apr 18 '15 at 02:12