Reputation: 37
package com.example.mayank.sunshine;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
/**
* A {@link PreferenceActivity} that presents a set of application settings.
* <p>
* See <a href="http://developer.android.com/design/patterns/settings.html">
* Android Design: Settings</a> for design guidelines and the <a
* href="http://developer.android.com/guide/topics/ui/settings.html">Settings
* API Guide</a> for more information on developing a Settings UI.
*/
public class SettingsActivity extends PreferenceFragment
implements Preference.OnPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Add 'general' preferences, defined in the XML file
addPreferencesFromResource(R.xml.pref_general);
// For all preferences, attach an OnPreferenceChangeListener so the UI summary can be
// updated when the preference changes.
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));
}
/**
* Attaches a listener so the summary is always updated with the preference value.
* Also fires the listener once, to initialize the summary (so it shows up before the value
* is changed.)
*/
private void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(this);
// Trigger the listener immediately with the preference's
// current value.
onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list (since they have separate labels/values).
ListPreference listPreference = (ListPreference) preference;
int prefIndex = listPreference.findIndexOfValue(stringValue);
if (prefIndex >= 0) {
preference.setSummary(listPreference.getEntries()[prefIndex]);
}
} else {
// For other preferences, set the summary to the value's simple string representation.
preference.setSummary(stringValue);
}
return true;
}
}
The above code shows the following error: Caused by: java.lang.ClassCastException: com.example.mayank.sunshine.SettingsActivity cannot be cast to android.app.Activity
The code previously had "SettingsActivity extends PreferenceActivity", but some of the functions are shown as deprecated, and I was guided to use the PreferenceFragment class instead, so on changing the parent class, the error started popping up. Also, the following part of manifest file shows error in android:name=".SettingsActivity"
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings"
android:parentActivityName=".MainActivity"
android:theme="@style/AppTheme.NoActionBar" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.mayank.sunshine.MainActivity" />
</activity>
Upvotes: 0
Views: 833
Reputation: 1006634
The above code shows the following error: Caused by: java.lang.ClassCastException: com.example.mayank.sunshine.SettingsActivity cannot be cast to android.app.Activity
That is because your SettingsActivity
does not have Activity
in its inheritance chain. SettingsActivity
inherits from Fragment
, which inherits from Object
.
The code previously had "SettingsActivity extends PreferenceActivity", but this class is deprecated in newer APIs
PreferenceActivity
is not deprecated in any version of Android, through API Level 23.
Upvotes: 1