3

I know this might be a duplicate from: What to use instead of "addPreferencesFromResource" in a PreferenceActivity?

But I still can not get the code to work and would appreciate some help. I can not get the: "addPreferencesFromResource(R.xml.preferences);" to work.

preferences is not resolved, even though it is in the R.xml folder.

I followed the example in the above link to the letter.

Please help!!

package com.example.oasisreference;

import android.R;
import android.R.xml;
//import android.annotation.TargetApi;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;

public class Prefs extends PreferenceActivity  {

@Override
protected void onCreate(final Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    getFragmentManager().beginTransaction().replace(android.R.id.content, new   MyPreferenceFragment()).commit();
}

public static class MyPreferenceFragment extends PreferenceFragment
{
@Override
public void onCreate(final Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
}

}

}

preferences.xml in the Res.xml folder

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

<EditTextPreference
    android:title="User Name"
    android:key="name"
    android:summary="Enter your name"
    ></EditTextPreference>
<CheckBoxPreference
    android:title="Euro Currency"
    android:defaultValue="false"
    android:key="euro"
    android:summary="Check for Use Cost Calculator to be in Euros/Liters">
 </CheckBoxPreference>


</PreferenceScreen>

menu.xml

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


<item
    android:id="@+id/aboutUs"
    android:title="About the Author">
</item>
<item
    android:id="@+id/preferences"
    android:title="Preferences">
</item>
<item 
    android:id="@+id/exit"
    android:title="Exit">
</item>

</menu>
Community
  • 1
  • 1
user2152750
  • 81
  • 1
  • 7

3 Answers3

0

delete import android.R it will work

Payam30
  • 689
  • 1
  • 5
  • 20
0

The answer is, it's not actually deprecated.... They just want you to use preferenceFragments (see below)

It turns out that it's the same routine name but it is instantiated as part of a preferenceFragment which is a different class. There's it's not deprecated. This is subtle but the examples at the developer website reference it from within a preference fragment. Paste the deprecated call into a preferenceFragment class and see for yourself the deprecation goes away

In the preferenceFragment class declaration:

package android.preference;

public abstract class PreferenceFragment extends android.app.Fragment {

// ...
    public void addPreferencesFromIntent(android.content.Intent intent) {
        /* compiled code */
    }
       
    public void addPreferencesFromResource(int preferencesResId) {
        /* compiled code */
    }
    // ...

and in the other class

package android.preference;

public abstract class PreferenceActivity extends android.app.ListActivity implements android.preference.PreferenceFragment.OnPreferenceStartFragmentCallback {

    /**
    * @deprecated
    */
@java.lang.Deprecated
public void addPreferencesFromResource(int preferencesResId) {
    /* compiled code */
}
royhowie
  • 11,075
  • 14
  • 50
  • 67
smrg
  • 1
  • 1
-1

Add @SuppressWarnings("deprecation") before OnCreate method.

sashkello
  • 17,306
  • 24
  • 81
  • 109
Guru
  • 9
  • 1