Java 在片段中第一次调用SharedReference时为空
我有一个示例android应用程序,根据设置(SharedReference)中设置的位置(邮政编码)和温度单位,该应用程序显示7天的天气 似乎当应用程序第一次获取温度并检查SharedReference中设置的温度单位时,它是空的,isMetric设置为TRUE。Utility.isMetric可以改进为表示没有从SharedReference获取数据,但我的问题是,为什么从ForecastFragment的onCreateView首次调用Utility.isMetric时SharedReference是空的 Utility.isMetric访问SharedReferenceJava 在片段中第一次调用SharedReference时为空,java,android,android-fragments,android-sharedpreferences,Java,Android,Android Fragments,Android Sharedpreferences,我有一个示例android应用程序,根据设置(SharedReference)中设置的位置(邮政编码)和温度单位,该应用程序显示7天的天气 似乎当应用程序第一次获取温度并检查SharedReference中设置的温度单位时,它是空的,isMetric设置为TRUE。Utility.isMetric可以改进为表示没有从SharedReference获取数据,但我的问题是,为什么从ForecastFragment的onCreateView首次调用Utility.isMetric时SharedRefe
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
ForecastFragment调用实用程序.isMetric
boolean isMetric = Utility.isMetric(getActivity());
我有一个logcat显示了这种行为,如果你想看到,请告诉我
由于字符数有最大限制,可以在以下位置访问完整的代码:
前甲板碎片
public class ForecastFragment extends Fragment implements LoaderCallbacks<Cursor> {
private SimpleCursorAdapter mForecastAdapter;
private static final int FORECAST_LOADER = 0;
private String mLocation;
// For the forecast view we're showing only a small subset of the stored data.
// Specify the columns we need.
private static final String[] FORECAST_COLUMNS = {
// In this case the id needs to be fully qualified with a table name, since
// the content provider joins the location & weather tables in the background
// (both have an _id column)
// On the one hand, that's annoying. On the other, you can search the weather table
// using the location set by the user, which is only in the Location table.
// So the convenience is worth it.
WeatherEntry.TABLE_NAME + "." + WeatherEntry._ID,
WeatherEntry.COLUMN_DATETEXT,
WeatherEntry.COLUMN_SHORT_DESC,
WeatherEntry.COLUMN_MAX_TEMP,
WeatherEntry.COLUMN_MIN_TEMP,
LocationEntry.COLUMN_LOCATION_SETTING
};
// These indices are tied to FORECAST_COLUMNS. If FORECAST_COLUMNS changes, these
// must change.
public static final int COL_WEATHER_ID = 0;
public static final int COL_WEATHER_DATE = 1;
public static final int COL_WEATHER_DESC = 2;
public static final int COL_WEATHER_MAX_TEMP = 3;
public static final int COL_WEATHER_MIN_TEMP = 4;
public static final int COL_LOCATION_SETTING = 5;
public ForecastFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Add this line in order for this fragment to handle menu events.
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.forecastfragment, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_refresh) {
updateWeather();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final String[] columns = {WeatherEntry.COLUMN_DATETEXT,
WeatherEntry.COLUMN_SHORT_DESC,
WeatherEntry.COLUMN_MAX_TEMP,
WeatherEntry.COLUMN_MIN_TEMP
};
final int[] viewIDs = {R.id.list_item_date_textview,
R.id.list_item_forecast_textview,
R.id.list_item_high_textview,
R.id.list_item_low_textview
};
// The SimpleCursorAdapter will take data from the database through the
// Loader and use it to populate the ListView it's attached to.
mForecastAdapter = new SimpleCursorAdapter(
getActivity(),
R.layout.list_item_forecast,
null,
columns,
viewIDs,
0);
mForecastAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
boolean isMetric = Utility.isMetric(getActivity());
switch (columnIndex) {
case COL_WEATHER_MAX_TEMP: {
String high = Utility.formatTemperature(
cursor.getDouble(cursor.getColumnIndex(WeatherEntry.COLUMN_MAX_TEMP)), isMetric);
((TextView) view).setText(high);
return true;
}
case COL_WEATHER_MIN_TEMP: {
// we have to do some formatting and possibly a conversion
String low = Utility.formatTemperature(
cursor.getDouble(cursor.getColumnIndex(WeatherEntry.COLUMN_MIN_TEMP)), isMetric);
((TextView) view).setText(low);
return true;
}
case COL_WEATHER_DATE: {
String dateString = Utility.formatDate(
cursor.getString(cursor.getColumnIndex(WeatherEntry.COLUMN_DATETEXT)));
((TextView) view).setText(dateString);
return true;
}
}
return false;
}
});
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
// Get a reference to the ListView, and attach this adapter to it.
ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
listView.setAdapter(mForecastAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Cursor cursor = mForecastAdapter.getCursor();
if (cursor != null && cursor.moveToPosition(position)) {
Intent intent = new Intent(getActivity(), DetailActivity.class)
.putExtra(DetailActivity.DATE_KEY, cursor.getString(COL_WEATHER_DATE));
startActivity(intent);
}
}
});
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
getLoaderManager().initLoader(FORECAST_LOADER, null, this);
super.onActivityCreated(savedInstanceState);
}
private void updateWeather() {
String location = Utility.getPreferredLocation(getActivity());
// update weather only if location is not an EMPTY string
if (location != null
&& location.length() > 0) {
new FetchWeatherTask(getActivity()).execute(location, SettingsActivity.FORECAST_DAYS);
}
}
@Override
public void onResume() {
super.onResume();
if (mLocation != null && !mLocation.equals(Utility.getPreferredLocation(getActivity()))) {
getLoaderManager().restartLoader(FORECAST_LOADER, null, this);
}
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// This is called when a new Loader needs to be created. This
// fragment only uses one loader, so we don't care about checking the id.
// To only show current and future dates, get the String representation for today,
// and filter the query to return weather only for dates after or including today.
// Only return data after today.
String startDate = WeatherContract.getDbDateString(new Date());
// Sort order: Ascending, by date.
String sortOrder = WeatherEntry.COLUMN_DATETEXT + " ASC";
mLocation = Utility.getPreferredLocation(getActivity());
if (mLocation == null || mLocation.length() == 0) {
mLocation = "00000";
}
Uri weatherForLocationUri = WeatherContract.WeatherEntry.buildWeatherLocationWithStartDate(mLocation, startDate);
Log.d(getString(R.string.app_name), weatherForLocationUri.toString());
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
return new CursorLoader(
getActivity(),
weatherForLocationUri,
FORECAST_COLUMNS,
null,
null,
sortOrder
);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mForecastAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mForecastAdapter.swapCursor(null);
}
}
我已经将logcat放在两个文件(实用工具和ForecastFragment)中来演示这个问题。这是安装应用程序后,ForecastFragment onCreateView首次加载时的日志
07-05 17:35:57.562 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:35:57.563 21604-21604/com.example.android.weather.app D/JIGAR: isMetric true
这是在按下刷新按钮后加载ForecastFragment onCreateView时的logcat
07-05 17:36:44.433 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.435 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.435 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.436 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.445 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.449 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.450 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.452 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.458 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.458 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.458 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.458 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.459 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.460 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.460 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.460 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.461 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.461 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.461 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.461 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.462 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.462 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.462 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.463 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.463 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.464 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.464 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.464 21604-21604/com.example.android.weather.app D/JIGAR: {}
现在我已经从上面两个位置删除了Log.d语句,但是非常欢迎您放置Log语句
注意:这是一个家庭作业应用程序,文件和完整的应用程序中显示了待办事项,但在玩应用程序和理解代码时,我在应用程序中发现了这个错误,并试图理解它发生的原因。这不是作业或任何提交的一部分。您的共享引用为空的原因是您没有成功输入任何内容 我建议以这种方式使用SharedReferences,因为这对我很有用:
import android.content.SharedPreferences;
在你的职能中:
SharedPreferences preferences = getApplicationContext().getSharedPreferences("AppPref", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("**Input your data here**", **tag your data/give it a name*);//example editor.putString("userPassword", password);
editor.commit();//this one places your string into the sharedpreferences instance you created
要在创建时访问共享首选项,请执行以下操作:
private String data; //this receives your data from sharedpref
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
SharedPreferences conn = getSharedPreferences("AppPref", MODE_PRIVATE);
data = conn.getString("**your data tag**","");
}
希望这有帮助。如果希望存储会话中的值,则需要专门为运行时输入的数据创建SharedReferences实例。我不太擅长技术上的解释,所以如果你有任何问题,请澄清 谢谢Dennis和Kaze,我的android应用程序在
addPreferencesFromResource(R.xml.pref_general)的帮助下正在从xml文件加载默认共享首选项
在SettingActivity中,扩展首选项activity
这是一个单独的活动,当有人单击“设置”按钮时启动,因此在按下“设置”按钮之前不会填充共享首选项
您关于填充默认首选项的问题使我了解了它是如何填充的,并且应该使用PreferenceManager在ForecastFragmentonCreate
中填充它。setDefaultValues
如下所示
PreferenceManager.setDefaultValues(context, R.xml.pref_general, false);
这解决了我的问题。我将投票支持你的答案!谢谢您应该使用PreferenceManager中的以下方法从xml加载默认值(如您在注释中所述,默认值是从xml加载的)
亲爱的投票人,如果你能至少说几句话,我会非常感激你为什么投票否决这个问题,这样可以帮助我和其他看到这个问题的人理解为什么投票被否决?它包含所有数据、我尝试过的内容和我正在寻找的内容,因此请用几句话说明为什么在不理解问题的情况下否决投票。调用
SharedReferences.getString(…)
或任何您想在onCreate(…)中获取的值
您首先将数据放在SharedReferences的何处?尝试在创建时获取SharedReferences,但无效。仅供参考-默认共享首选项完全从pref_general.xml.yea读取!这就是我在评论部分要求他做的事。:)我试图发表评论,但正如你所看到的。。我没有足够的代表。我在回复时没有刷新,所以我看不到哈哈。我只是不希望那些真正需要帮助的人被否决。是的,数据是在不同的活动中加载的,只有在单击设置按钮时才会加载,所以我必须在片段中从xml填充默认的共享首选项。谢谢你的指点!Np,看看你是否可以投票表决这个问题,这样它就不会是否定的哈哈哈。我昨天没有投票,所以给出了不止一个答案:)确保你在主要活动的onCreate()中这样做
PreferenceManager.setDefaultValues(context, R.xml.pref_general, false);
static void setDefaultValues(Context context, int resId, boolean readAgain)
Sets the default values from an XML preference file by reading the values defined by each Preference item's android:defaultValue attribute.