0
在使用我的小部件中的共享首選項時遇到了一個小問題。我不斷收到我的共享首選項的調試消息,但ID的敵人不會出現錯誤。Shared Preference上沒有文件或目錄
unable to unlink'/data/data/com.MyApps.WeatherViewer/shared_prefs/USER_PREFERENCE_WEATHER.xml.bak': No such file or directory (errno=2)
什麼可能是問題cos當我嘗試訪問它們時,存儲在我的共享首選項中的值將返回null。 ?
2)是否有可能從一個共享偏好檢索值immediatelyfrom創建它們的活動..
這裏是我創建SharedPreference代碼:
public static final String USER_PREFERENCE_MODE = "USER_PREFERENCES_WEATHER";
public static final String PREF_CITY_ENTRY = "PREF_CITY_ENTRY";
public static final String PREF_TOGGLE_CELSIUS = "PREF_TOGGLE_CELSIUS";
public static final String PREF_FREQ_UPDATE = "PREF_FREQ_UPDATE";
SharedPreferences prefs;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.weather_view_configuration);
cityText =(EditText)findViewById(R.id.editCity_id);
chk_celsius = (CheckBox) findViewById(R.id.celsius_toggle_id);
save =(Button)findViewById(R.id.SaveButton);
update =(Spinner)findViewById(R.id.spinner_update_id);
cityText.setOnClickListener(this);
save.setOnClickListener(this);
prefs = this.getSharedPreferences(USER_PREFERENCE_MODE,0);
updateUIFromPreferences();
}
@Override
public void onClick(View v) {
final Context context = WeatherForecastConfigure.this;
int selection = v.getId();
switch(selection){
case R.id.SaveButton:
savePreferences();
prefs = getSharedPreferences(USER_PREFERENCE_MODE,0);
RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.weather_appwidget);
String text = prefs.getString(WeatherForecastConfigure.PREF_CITY_ENTRY,null);
views.setTextViewText(R.id.weather_city_id, text);
AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
widgetManager.updateAppWidget(appWidgetID, views);
}
}
private void updateUIFromPreferences() {
String cityEntry = prefs.getString(PREF_CITY_ENTRY, cityText.getText().toString());
boolean celsiusToggle = prefs.getBoolean(PREF_TOGGLE_CELSIUS, false);
int updateIndex = prefs.getInt(PREF_FREQ_UPDATE, 0);
cityText.setText(cityEntry);
update.setSelection(updateIndex);
chk_celsius.setChecked(celsiusToggle);
}
private void savePreferences() {
prefs = this.getSharedPreferences(USER_PREFERENCE_MODE,0);
int updateIndex = update.getSelectedItemPosition();
boolean celsiusToggle = chk_celsius.isChecked();
String city = cityText.getText().toString();
Editor editor = prefs.edit();
editor.putString(PREF_CITY_ENTRY, city);
editor.putBoolean(PREF_TOGGLE_CELSIUS, celsiusToggle);
editor.putInt(PREF_FREQ_UPDATE, updateIndex);
editor.commit();
}
在此先感謝
這裏是我的服務代碼:
public class weatherForecast extends Service {
public static final String UPDATE = "update";
int appWidgetID = AppWidgetManager.INVALID_APPWIDGET_ID;
String city;
Boolean chk_celsius;
SharedPreferences prefs;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onCreate(){
Log.d(TAG, "onCreate");
prefs = getApplicationContext().getSharedPreferences(WeatherForecastConfigure.USER_PREFERENCE_MODE, 0);
city = prefs.getString(WeatherForecastConfigure.PREF_CITY_ENTRY,"");
chk_celsius = prefs.getBoolean(WeatherForecastConfigure.PREF_TOGGLE_CELSIUS, false);
Log.i(TAG, "city is " + city);
}
public void onStart(Intent intent, int startId){
URL url;
Log.d(TAG,"onStart");
Bundle extra = intent.getExtras();
if(extra != null){
appWidgetID = extra.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
Log.v("WIDGET_TAG_ID", "appWidgetId = " + appWidgetID);
}
/* prefs = this.getSharedPreferences("prefs", 0);
city = prefs.getString(WeatherForecastConfigure.PREF_CITY_ENTRY + appWidgetID,"");
chk_celsius = prefs.getBoolean(WeatherForecastConfigure.PREF_TOGGLE_CELSIUS, false);
Log.i(TAG, "city is " + city);*/
RemoteViews updateViews = buildUpdate(this);
ComponentName thisWidget = new ComponentName(this, WeatherWidgetProvider.class);
AppWidgetManager widgetManager = AppWidgetManager.getInstance(this);
widgetManager.updateAppWidget(thisWidget, updateViews);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.onStart(intent, startId);
}
public RemoteViews buildUpdate(Context context) {
GoogleWeatherHandler gwh = new GoogleWeatherHandler();
WeatherSet weather = gwh.getWeatherSet();
RemoteViews remote = RemoteViews(context.getPackageName(), R.layout.weather_appwidget);
AppWidgetManager widgetManager = AppWidgetManager.getInstance(this);
remote.setTextViewText(R.id.editCity_id,weather.getCondition().getCity().toString());
remote.setTextViewText(R.id.weather_condition_id,weather.getCondition().getCondition().toString());
remote.setTextViewText(R.id.weather_tempreture_id,weather.getCondition().getTempCelcius().toString());
widgetManager.updateAppWidget(appWidgetID, views);
return views;
}
}
第一個問題 - 它似乎無法找到一個名爲/data/.../USER_PREFERENCE_WEATHER.xml.bak的文件。你有(在任何時候)手動創建了一個xml的副本,給它.bak擴展名,然後手動刪除它? – Squonk 2010-12-04 16:22:51
對不起..我剛剛看到你的評論。不,我從來沒有做過這樣的事情。我最近創建了一個名爲USER_PREFERENCE的應用程序,並且我爲這個應用程序使用了相同的名稱方案。但是當我不斷收到相同的錯誤時,我現在將其更改爲它當前的狀態(USER_PREFERENCE_WEATHER)..但我仍然遇到同樣的問題。 – irobotxxx 2010-12-05 03:22:22