java - 由 : java. lang.ClassCastException : com. example.mayank.sunshine.SettingsActivity 引起,无法转换为 android.app.Activity

标签 java android android-studio

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;
    }

}

上面的代码显示以下错误: 引起原因:java.lang.ClassCastException:com.example.mayank.sunshine.SettingsActivity无法转换为android.app.Activity

之前的代码有“SettingsActivity extends PreferenceActivity”,但某些功能显示为已弃用,并且我被引导使用 PreferenceFragment 类,因此在更改父类时,开始弹出错误。此外, list 文件的以下部分在 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>

最佳答案

The above code shows the following error: Caused by: java.lang.ClassCastException: com.example.mayank.sunshine.SettingsActivity cannot be cast to android.app.Activity

那是因为您的SettingsActivity在其继承链中没有ActivitySettingsActivity 继承自 Fragment,而 Fragment 又继承于 Object

The code previously had "SettingsActivity extends PreferenceActivity", but this class is deprecated in newer APIs

PreferenceActivity 在任何版本的 Android 中都不会被弃用,直到 API 级别 23。

关于java - 由 : java. lang.ClassCastException : com. example.mayank.sunshine.SettingsActivity 引起,无法转换为 android.app.Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33088117/

相关文章:

android - 检测新 SIM 卡何时插入设备

android.tools.build :gradle:3. 0.1 - 无法获取未知属性 'processResourcesTask'

java - 不带 else 的多个 if 语句

java - Java 中的 ISO-8859-1 到 UTF-8 (runescape API)

java - XWPFDocument - getPageCount

android-studio - ANDROID_SDK_ROOT=undefined(推荐设置),同时使用 Cordova for Android 设备构建 ionic 应用程序

java - 在android studio中引用菜单itemId

java - 是否可以在不调用 getContentResolver() 的情况下从 Uri 检索位图?

android - 调用 MobileAds.initialize() 的正确方法是什么?

用于 Spannable 的 Android 填充?