2

我有一個示例android應用程序,取決於設置(SharedPreference)中設置的位置(郵編)和溫度單位,應用程序顯示7天的天氣。SharedPreferences在片段中第一次被調用時爲空

看起來應用程序第一次獲取溫度並檢查SharedPreference中設置的溫度單位是​​多少,它是空的,isMetric設置爲TRUE。 Utility.isMetric可以被改進來表示沒有從SharedPreference獲取的數據,但是我的問題是爲什麼當從ForecastFragment的onCreateView的Utility.isMetric中第一次調用SharedPreference時爲空?

Utility.isMetric訪問SharedPreference

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 

ForecastFragment調用Utility.isMetric

boolean isMetric = Utility.isMetric(getActivity()); 

我有logcat的,顯示這種行爲,讓我知道,如果你想看到這一點。

由於沒有最大限制。字符的,完整的代碼可以在https://github.com/gosaliajigar/CSC519/tree/master/CSC519-HW6

ForecastFragment

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); 
    } 
} 

實用

public class Utility { 
    public static String getPreferredLocation(Context context) { 
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 
     return prefs.getString(context.getString(R.string.pref_location_key), 
       context.getString(R.string.pref_location_default)); 
    } 

    public static boolean isMetric(Context context) { 
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 
     return prefs.getString(context.getString(R.string.pref_units_key), 
       context.getString(R.string.pref_units_metric)) 
       .equals(context.getString(R.string.pref_units_metric)); 
    } 

    static String formatTemperature(double temperature, boolean isMetric) { 
     double temp; 
     if (!isMetric) { 
      temp = 9*temperature/5+32; 
     } else { 
      temp = temperature; 
     } 
     return String.format("%.0f", temp); 
    } 

    static String formatDate(String dateString) { 
     Date date = WeatherContract.getDateFromDb(dateString); 
     return DateFormat.getDateInstance().format(date); 
    } 
} 

我已經把logcat的兩個文件(實用& ForecastFragment)來演示該問題進行訪問。當應用程序安裝時加載ForecastFragment onCreateView時,這是第一次使用logcat。

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語句,但是您不僅歡迎您放置日誌語句。

注意:這是一個帶有TO DO功能的應用程序,現在顯示在文件及其完整應用程序中,但是在使用應用程序和理解代碼時,我在應用程序中發現了此錯誤並試圖瞭解其發生的原因。這是NOT功課或任何提交的一部分。

+0

親愛下來的選民,我會很感激,如果你能ATLEAST下降幾句話你爲什麼投下來的問題,這樣會幫助我和其他人誰看到這個問題理解爲什麼下來投票?它包含所有數據,我試過的以及我在尋找什麼,所以請滴滴答答地說,爲什麼不投票理解問題就投了票。 – JRG

+0

通話要在獲取了'SharedPreferences.getString(..)'或任何價值'的onCreate(...)' – Dennis

+0

和你在哪裏把數據在SharedPreferences擺在首位? – Dennis

回答

2

感謝丹尼斯和風,我的Android應用程序加載從XML文件中的默認共享偏好與SettingActivity的addPreferencesFromResource(R.xml.pref_general)幫助下extends PreferenceActivity這是一個單獨的活動,當有人點擊設置按鈕,它啓動,因此共享偏好從來沒有在按設置按鈕之前填充。

你有關填充默認的喜好問題讓我看看它是如何填充,它應該在ForecastFragment onCreate使用PreferenceManager.setDefaultValues如下填充...

PreferenceManager.setDefaultValues(context, R.xml.pref_general, false); 

這解決了我的問題。我會投票答覆你的答案!由於

2

你的sharedPreferences爲空的原因是你沒有成功地輸入任何東西。

我建議使用sharedPreferences這種方式,因爲這是對我工作:

在功能
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 

才能訪問你的共享偏好的onCreate:

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**",""); 
} 

希望這有助於。如果您希望存儲會話中的值,則需要專門爲運行期間輸入的數據創建一個sharedpreferences實例。我不擅長技術解釋,所以如果你有任何問題,請澄清一下。

+1

是啊!這就是我要求他在評論部分做的。 :) – Dennis

+1

我想發表評論,但你可以看到。我沒有足夠的代表。當我給出迴應時我沒有刷新,所以我看不到哈哈。我只是不希望真正需要幫助的人得到低估。其粗糙的人。 –

+0

是的,數據被加載到不同的活動中,只有當點擊設置按鈕時才加載數據,所以我必須從片段中的xml填充默認共享首選項。感謝指針! Np,看看你是否可以投票的問題,所以它不是負面哈哈哈。 – JRG

1

您應該使用下列方法從PreferenceManager從XML加載默認值(如你在你的註釋說明了缺省值從XML加載)。

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. 
+1

請確保你在MainActivity的onCreate() – user8294505

相關問題