2010-12-04 17 views
0

私のウィジェットの共有設定を使用する際に若干の問題が発生しています。私は共有の設定でこのデバッグメッセージを取得し続けますが、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)は、共有設定がそれらを作成した活動を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; 
    } 
} 
+0

最初の問題では、/data/.../USER_PREFERENCE_WEATHER.xml.bakというファイルが見つからないようです。あなたは(いつでも)xmlのコピーを手作業で作成して.bak拡張子を与えてから、後で手動で削除しましたか? – Squonk

+0

あなたのコメントを見ました。私は決してそのようなことはしませんでした。私は最近、USER_PREFERENCEという名前を使用したアプリケーションを作成しました。このアプリケーションには同じ名前のスキームを使用しました。しかし、私は同じエラーが発生したとき、私は今それが現在のもの(USER_PREFERENCE_WEATHER)に変更されました..しかし、同じ問題を抱えています。 – irobotxxx

答えて

0

それはリクを探しますあなたのコードは、より良い言葉の欠如のために「循環問題」のような形で捕らえられます。

たとえば、あなたがして

String cityEntry = prefs.getString(PREF_CITY_ENTRY, cityText.getText().toString()); 
cityText.setText(cityEntry); 

あなたのあなたは実際の値にcityTextを設定しない場合

String city = cityText.getText().toString(); 
editor.putString(PREF_CITY_ENTRY, city); 

を行うボタンを保存しない

updateUIFromPreferences(); 

を呼び出しますヌル値を環境設定に戻すだけです。私は全体の活動なしで確信することはできませんが、これはそれが見えることからの問題です。

+0

遅れて申し訳ありませんが、サービスやappWidgetプロバイダクラスから共有設定にアクセスしようとすると、値がnullになります。これはわかりません。ところで、それは私がそこに持っている活動のおおよそのことです。 – irobotxxx

+0

私は何を意味したのですか?updateFromPreferenceはoncreateメソッドで呼び出されます。だから私は値を入力し、それを保存した後、実際に読み込まれます。他のサービスやappWidgetクラスからはnullが返されます。 – irobotxxx

+0

その後、サービスコードを掲示してください。 – user432209

関連する問題