gpt4 book ai didi

java - 重构设置 Activity

转载 作者:行者123 更新时间:2023-12-02 04:44:14 25 4
gpt4 key购买 nike

我在重构 SettingsActivity.java 时遇到问题。

首先,我扩展了 PreferenceActivity 但它没有花费addPreferencesFromResource(R.xml.pref_general) 因为它标记已弃用以及 findPreference(getString(R.string.pref_location_key)。我已更改为 PreferenceFragment 并且它采用了上述两种方法,但现在无法解析 getMenuInflater().inflate(R. menu.menu_settings,菜单);

此外,它还显示:“无法解析符号 pref_units_keys”bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_units_key)));

最后在 ForecastFragment.java 中我无法正确使用此方法:

private String formatHighLows(double high, double low){
// Data is fetched in Celsius by default.
// If user prefers to see in Fahrenheit, convert the values here.
// We do this rather than fetching in Fahrenheit so that the user can
// change this option without us having to re-fetch the data once
// we start storing the values in the database
SharedPreferences sharedPrefs =
PreferenceManager.getDefaultSharedPreferences(getActivity());
String unitType = sharedPrefs.getString(
getString(R.string.pref_units_key),
getString(R.string.pref_units_metric));

if(unitType.equals(getString(R.string.pref_units_imperial))){
high = (high * 1.8) + 32;
low = (low * 1.8) + 32;
}else if(!unitType.equals(R.string.pref_units_metric)){
Log.d(LOG_TAG, "Unit type not found: " + unitType);
}

可能我需要在 string.xml 文件中声明单位和公制...

我能做什么?

以下是整个代码的链接:http://pastebin.com/KJRbj6Sd#

最佳答案

您可能需要获取 Activity 引用。

尝试添加getActivity()之前getMenuInflater() ,和getResources()在您的每一个 getString() 之前s。

private String formatHighLows(double high, double low){

SharedPreferences sharedPrefs =
PreferenceManager.getDefaultSharedPreferences(getActivity());
String unitType = sharedPrefs.getString(
getResources().getString(R.string.pref_units_key),
getResources().getString(R.string.pref_units_metric));

if(unitType.equals(getResources().getString(R.string.pref_units_imperial))){
high = (high * 1.8) + 32;
low = (low * 1.8) + 32;
}else if(!unitType.equals(getResources().getString(R.string.pref_units_metric))){
Log.d(LOG_TAG, "Unit type not found: " + unitType); }
}

关于java - 重构设置 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29810723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com