简体   繁体   中英

How can I show a preferences screen on older Android devices i.e. Gingerbread and Froyo?

I need to show a preferences screen from my application. I've managed to implement this and it works on Ice-Cream Sandwich and newer devices but doesn't work on older devices like Gingebread and Froyo. I'm using ActionBarSherlock as a compatibility library to support older devices.

Here's the code for the Activity that contains the preference screen:

public class Settings extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    
        getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new Preferences())
                .commit();
    }

}

This is the sample code from my activity that invokes the preferences screen:

public class Trend extends SherlockActivity {

    @Override
    public boolean onOptionsItemSelected(MenuItem itmMenuitem) {
        if (itmMenuitem.getItemId() == R.id.search) {
            startActivity(new Intent(getBaseContext(), Settings.class));
            return true;
        }
    }

}

When users try and open the preferences screen on older devices, I get an exception that reads:

java.lang.NoSuchMethodError: com.mridang.stupidapp.Settings.getFragmentManager
at com.mridang.stupidapp.Settings.onCreate(Settings.java:15)

How should I modify my preferences screen to work with older devices ie API 8? Thanks.

Neither ActionBarSherlock nor the Android Support Library provides support for Preference Fragments.

You need to revert to Perference Activities as explained in the Settings Documentation .

There are a few old questions that discuss the same topic:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM