我在android中创建了一个设置活动。我正在使用下面的代码。
public class SettingsActivity extends SherlockPreferenceActivity implements OnSharedPreferenceChangeListener, OnPreferenceClickListener {
QuoteOfTheDayAlarmManager alert;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
alert = new QuoteOfTheDayAlert();
addPreferencesFromResource(R.xml.preferences);
this.updateTimeSummary(this.findPreference("qotd_notification_time"),
super.getPreferenceScreen().getSharedPreferences().getInt("qotd_notification_time.hour", 8),
super.getPreferenceScreen().getSharedPreferences().getInt("qotd_notification_time.minute", 0),
super.getPreferenceScreen().getSharedPreferences().getBoolean("qotd_show_notification", false));
this.findPreference("share_settings_intent").setOnPreferenceClickListener(this);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@SuppressWarnings("deprecation")
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPrefs, String key) {
if (key.equals("qotd_show_notification")) {
if (sharedPrefs.getBoolean("qotd_show_notification", false)) {
alert.setAlarm(this.getBaseContext());
} else {
alert.cancelAlarm(this.getBaseContext());
}
} else if (key.equals("qotd_notification_time.hour") ||
key.equals("qotd_notification_time.minute")) {
this.updateTimeSummary(this.findPreference("qotd_notification_time"),
sharedPrefs.getInt("qotd_notification_time.hour", 8),
sharedPrefs.getInt("qotd_notification_time.minute", 0),
sharedPrefs.getBoolean("qotd_show_notification", false));
}
}
@SuppressWarnings("deprecation")
@Override
protected void onResume() {
super.onResume();
super.getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
this.findPreference("share_settings_intent").setOnPreferenceClickListener(this);
}
@SuppressWarnings("deprecation")
@Override
protected void onPause() {
super.onPause();
super.getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem)
{
switch(menuItem.getItemId()) {
case android.R.id.home:
finish();
break;
default:
return super.onOptionsItemSelected(menuItem);
}
return true;
}
@Override
public void finish() {
Intent intent=new Intent();
intent.putExtra("ComingFrom", "Settings");
setResult(RESULT_OK, intent);
super.finish();
}
/**
* Nice display of the hour values
* @param pref
* @param hour
* @param minute
*/
private void updateTimeSummary(Preference pref, int hour, int minute, boolean alertActive) {
TimePreference timePref = (TimePreference)pref;
String ampmFlag = (hour > 11) ? "PM" : "AM";
String minuteString = (minute < 10) ? "0" + String.valueOf(minute) : String.valueOf(minute);
String hourString = (hour > 12) ? String.valueOf(hour-12) : String.valueOf(hour);
timePref.setSummary(String.format("%s:%s %s", hourString, minuteString, ampmFlag));
//Also cancel and reset the alarm
alert.cancelAlarm(this.getBaseContext());
if (alertActive) { alert.setAlarm(this.getBaseContext()); }
}
@Override
public boolean onPreferenceClick(Preference pref) {
if (pref.getKey().equals("share_settings_intent")) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Value Investor Wisdom");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,
"I found this great app about Value Investing on Google Play! Check it out: https://play.google.com/store/apps/details?id=dcallaghan.valueinvestorwisdom");
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
return true;
}
}
此活动在2.3.x以上的Android版本中运行。但是当我在2.3.x中运行时,我得到错误
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.evilsofts.friendshipandlove/com.evilsofts.friendshipandlove.SettingsActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class SwitchPreference
任何人都可以帮助我为什么我在android 2.3.x中收到此错误?请帮帮我。
谢谢
答案 0 :(得分:0)
SwitchPreference
引入了API level 14
。它不适用于支持库,因此您无法在Android 2.3.x中使用它。
Here您可以找到SwitchPreference