Android 共享首选项上不存在文件或目录
我在小部件中使用共享首选项时遇到了一个小问题。我在共享首选项上不断收到此调试消息,但id敌人并不是一个错误Android 共享首选项上不存在文件或目录,android,Android,我在小部件中使用共享首选项时遇到了一个小问题。我在共享首选项上不断收到此调试消息,但id敌人并不是一个错误 unable to unlink'/data/data/com.MyApps.WeatherViewer/shared_prefs/USER_PREFERENCE_WEATHER.xml.bak': No such file or directory (errno=2) 当我尝试访问共享首选项时,存储在共享首选项中的值返回null,这可能是什么问题 2.)是否可以从创建共享首选项的活动中
unable to unlink'/data/data/com.MyApps.WeatherViewer/shared_prefs/USER_PREFERENCE_WEATHER.xml.bak': No such file or directory (errno=2)
当我尝试访问共享首选项时,存储在共享首选项中的值返回null,这可能是什么问题
2.)是否可以从创建共享首选项的活动中立即从共享首选项中检索值
以下是我创建SharedReference的代码:
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;
}
}
看起来您的代码陷入了某种“循环问题”,因为缺少更好的术语 例如,你打电话
updateUIFromPreferences();
哪一个
String cityEntry = prefs.getString(PREF_CITY_ENTRY, cityText.getText().toString());
cityText.setText(cityEntry);
String city = cityText.getText().toString();
editor.putString(PREF_CITY_ENTRY, city);
然后你的保存按钮
String cityEntry = prefs.getString(PREF_CITY_ENTRY, cityText.getText().toString());
cityText.setText(cityEntry);
String city = cityText.getText().toString();
editor.putString(PREF_CITY_ENTRY, city);
如果您从未使用实际值设置cityText,只需将空值传递回首选项即可。如果没有整个活动,我无法确定,但从外观上看,这就是问题所在。关于第一个问题,似乎找不到名为/data/../USER\u PREFERENCE\u WEATHER.xml.bak的文件。您是否(在任何时候)手动创建了xml的一个副本,将其扩展名设为.bak,然后在以后手动删除了它?对不起,我刚才看到了您的评论。不,我从来没有做过那样的事。我最近创建了一个使用名称USER_首选项的应用程序,并为此应用程序使用了相同的名称方案。但当我不断得到相同的错误时,我现在将其更改为当前状态(用户偏好天气)。。但是我还是有同样的问题。很抱歉反应太晚。。但是当我试图从服务或appWidget提供程序类访问共享首选项时,我得到的值为null,我不理解。顺便说一下,这大概就是我在那里的全部活动。我的意思是。。updateFromPreference在oncreate方法上被调用。因此,在我输入一个值并保存它之后,它实际上会加载。但是从另一个服务或appWidget类,我得到null。您好,我已经编辑了我的问题,并将服务代码放在那里。再次感谢您抽出时间。