這是我之後的第二個問題(已經回答):Creating SharedPreferences object inside Fragment。離開設置活動時App Force關閉
在弄清楚如何在靜態片段類中創建SharedPreferences對象後,我開始使用它從SettingsActivity中檢索用戶設置的值以設置TextEdits值。應用程序編譯沒有錯誤,並很好地啓動模擬器,顯示默認值(我還沒有輸入設置)。設置打開沒有任何問題。但是,當我在ActionBar上單擊「向上」時,應用程序掛起並強制關閉。之後,我無法打開它 - 它會一直關閉,直到清除應用程序數據。
它看起來像第一個應用程序打開Fragment靜態類中的SharedPreference方法設置默認數據(因爲他們應該,如果他們發現空),並在輸入設置後返回使用「向上」(我沒有改變任何值,只是保留默認的偏好xml中定義),應用程序將該默認值保存到SharedPreferences中,然後嘗試檢索它以在Fragments中設置TextEdits。並且它有關閉的地方。
奇怪的是,當我在靜態片段類(public static class Dzien extends Fragment)中使用/ * * /註釋關閉所有ifs時,應用程序工作,進入,離開Settings並且它是可以在不崩潰的情況下掃過所有碎片。更爲奇怪的是,「開啓」,如果在最後一種情況下5 ...:
case 5: { // Piątek
lekcja1.setText(getString(R.string.mat));
lekcja2.setText(getString(R.string.mat));
lekcja3.setText(getString(R.string.bio));
lekcja4.setText(getString(R.string.pol));
lekcja5.setText(getString(R.string.pol));
lekcja6.setText(getString(R.string.edb));
if (settings.getBoolean(WDZ, false)) {
lekcja7.setText(getString(R.string.wdz));
} else {
lekcja7.setText(getString(R.string.brak));
}
lekcja8.setText(getString(R.string.brak));
lekcja9.setText(getString(R.string.brak));
break;
}
...不作設置碰撞而留下他們,要5片段(其中使用WDZ )。我可以切換WDZ複選框,並在相應的片段中反映它的狀態。
我不知道什麼可能會導致崩潰,因爲如果情況1,2,3和4的塊是正確的 - 唯一的區別是他們使用getInt(),而不是getBoolean()。不過,導航到相應的片段後,應用程序崩潰。
下面是來源:
MainActivity.class(在底部片段類)
package pl.konpon96.planlekcji;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Calendar;
import java.util.Locale;
public class MainActivity extends FragmentActivity {
//public static final String APP_PREFERENCES = "app_preferences";
//public static final String GRUPA_ANGIELSKI = "grupa_angielski_list";
public static final String GRUPA_JEZYKOWA = "grupa_jezykowa_list";
public static final String GRUPA_INFORMATYKA = "grupa_informatyka_list";
public static final String WDZ = "wdz_checkbox";
//public static final String PODSWIETLANIE_LEKCJI = "podswietlanie_lekcji_checkboxs";
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
//Ustawianie domyślnej zakładki w zależności od dnia
Calendar timeNow = Calendar.getInstance();
switch (timeNow.get(Calendar.DAY_OF_WEEK)) {
case Calendar.MONDAY: {
mViewPager.setCurrentItem(0, true);
break;
}
case Calendar.TUESDAY: {
mViewPager.setCurrentItem(1, true);
break;
}
case Calendar.WEDNESDAY: {
mViewPager.setCurrentItem(2, true);
break;
}
case Calendar.THURSDAY: {
mViewPager.setCurrentItem(3, true);
break;
}
case Calendar.FRIDAY: {
mViewPager.setCurrentItem(4, true);
break;
}
case Calendar.SATURDAY: {
mViewPager.setCurrentItem(0, true);
Toast sobotaToast = Toast.makeText(getApplicationContext(),
"Jest sobota - nie ma zajęć", Toast.LENGTH_SHORT);
sobotaToast.show();
break;
}
case Calendar.SUNDAY: {
mViewPager.setCurrentItem(0, true);
Toast niedzielaToast = Toast.makeText(getApplicationContext(),
"Jest niedziela - nie ma zajęć", Toast.LENGTH_SHORT);
niedzielaToast.show();
break;
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_settings: {
Intent settingsActivity = new Intent(getBaseContext(), SettingsActivity.class);
startActivity(settingsActivity);
return true;
}
case R.id.action_dzwonki: {
Intent dzwonkiActivity = new Intent(getBaseContext(), DzwonkiActivity.class);
startActivity(dzwonkiActivity);
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new Dzien();
Bundle args = new Bundle();
args.putInt(Dzien.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
// Show 5 total pages.
return 5;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_pon).toUpperCase(l);
case 1:
return getString(R.string.title_wt).toUpperCase(l);
case 2:
return getString(R.string.title_sr).toUpperCase(l);
case 3:
return getString(R.string.title_czw).toUpperCase(l);
case 4:
return getString(R.string.title_pt).toUpperCase(l);
}
return null;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
/*
public static class DummySectionFragment extends Fragment {
* The fragment argument representing the section number for this
* fragment.
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
*/
public static class Dzien extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public Dzien() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView lekcja1 = (TextView) rootView.findViewById(R.id.lekcja1);
TextView lekcja2 = (TextView) rootView.findViewById(R.id.lekcja2);
TextView lekcja3 = (TextView) rootView.findViewById(R.id.lekcja3);
TextView lekcja4 = (TextView) rootView.findViewById(R.id.lekcja4);
TextView lekcja5 = (TextView) rootView.findViewById(R.id.lekcja5);
TextView lekcja6 = (TextView) rootView.findViewById(R.id.lekcja6);
TextView lekcja7 = (TextView) rootView.findViewById(R.id.lekcja7);
TextView lekcja8 = (TextView) rootView.findViewById(R.id.lekcja8);
TextView lekcja9 = (TextView) rootView.findViewById(R.id.lekcja9);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity());
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1: { // Poniedziałek
lekcja1.setText(getString(R.string.wos));
lekcja2.setText(getString(R.string.mat));
lekcja3.setText(getString(R.string.ang));
lekcja4.setText(getString(R.string.gw));
lekcja5.setText(getString(R.string.his));
lekcja6.setText(getString(R.string.wf));
lekcja7.setText(getString(R.string.pp));
if (settings.getInt(GRUPA_INFORMATYKA, 1) == 1) {
lekcja8.setText(getString(R.string.inf));
} else {
lekcja8.setText(getString(R.string.brak));
}
lekcja9.setText(getString(R.string.brak));
break;
}
case 2: { // Wtorek
lekcja1.setText(getString(R.string.mat));
lekcja2.setText(getString(R.string.pp));
lekcja3.setText(getString(R.string.rel));
lekcja4.setText(getString(R.string.wf));
lekcja5.setText(getString(R.string.pol));
lekcja6.setText(getString(R.string.pol));
if (settings.getInt(GRUPA_JEZYKOWA, 1) == 2) {
lekcja7.setText(getString(R.string.ros));
lekcja8.setText(getString(R.string.ros));
} else {
lekcja7.setText(getString(R.string.brak));
lekcja8.setText(getString(R.string.brak));
}
lekcja9.setText(getString(R.string.brak));
break;
}
case 3: { // Sroda
lekcja1.setText(getString(R.string.his));
lekcja2.setText(getString(R.string.wf));
lekcja3.setText(getString(R.string.mat));
lekcja4.setText(getString(R.string.rel));
lekcja5.setText(getString(R.string.ang));
if (settings.getInt(GRUPA_JEZYKOWA, 1) == 1) {
lekcja6.setText(getString(R.string.niem));
lekcja7.setText(getString(R.string.niem));
} else {
lekcja6.setText(getString(R.string.brak));
lekcja7.setText(getString(R.string.brak));
}
lekcja8.setText(getString(R.string.brak));
lekcja9.setText(getString(R.string.brak));
break;
}
case 4: { // Czwartek
lekcja1.setText(getString(R.string.mat));
lekcja2.setText(getString(R.string.fiz));
lekcja3.setText(getString(R.string.wok));
lekcja4.setText(getString(R.string.geo));
lekcja5.setText(getString(R.string.ang));
lekcja6.setText(getString(R.string.chem));
if (settings.getInt(GRUPA_INFORMATYKA, 1) == 2) {
lekcja7.setText(getString(R.string.inf));
} else if (((settings.getInt(GRUPA_INFORMATYKA, 1) == 2)) && ((settings.getInt(GRUPA_JEZYKOWA, 1) != 3))) {
lekcja7.setText(getString(R.string.brakpre));
} else {
lekcja7.setText(getString(R.string.brak));
}
if (settings.getInt(GRUPA_JEZYKOWA, 1) == 3) {
lekcja8.setText(getString(R.string.por));
lekcja9.setText(getString(R.string.por));
}
break;
}
case 5: { // Piątek
lekcja1.setText(getString(R.string.mat));
lekcja2.setText(getString(R.string.mat));
lekcja3.setText(getString(R.string.bio));
lekcja4.setText(getString(R.string.pol));
lekcja5.setText(getString(R.string.pol));
lekcja6.setText(getString(R.string.edb));
if (settings.getBoolean(WDZ, false)) {
lekcja7.setText(getString(R.string.wdz));
} else {
lekcja7.setText(getString(R.string.brak));
}
lekcja8.setText(getString(R.string.brak));
lekcja9.setText(getString(R.string.brak));
break;
}
}
return rootView;
}
}
}
SettingsActivity.class - 有除去預編譯的偏好片段後一點的混亂 - 我使用不推薦的API導致它不會使用平板電腦UI。
package pl.konpon96.planlekcji;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
/**
* A {@link PreferenceActivity} that presents a set of application settings. On
* handset devices, settings are presented as a single list. On tablets,
* settings are split by category, with category headers shown to the left of
* the list of settings.
* <p/>
* See <a href="http://developer.android.com/design/patterns/settings.html">
* Android Design: Settings</a> for design guidelines and the <a
* href="http://developer.android.com/guide/topics/ui/settings.html">Settings
* API Guide</a> for more information on developing a Settings UI.
*/
@SuppressWarnings("deprecation")
public class SettingsActivity extends PreferenceActivity {
private static final boolean ALWAYS_SIMPLE_PREFS = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupActionBar();
addPreferencesFromResource(R.xml.pref_general);
bindPreferenceSummaryToValue(findPreference("grupa_angielski_list"));
bindPreferenceSummaryToValue(findPreference("grupa_jezykowa_list"));
bindPreferenceSummaryToValue(findPreference("grupa_informatyka_list"));
}
/**
* Set up the {@link android.app.ActionBar}, if the API is available.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() { // Przycisk UP na ActionBar
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) { // Akcja użytkownika - przycisk UP
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
// TODO: If Settings has multiple levels, Up should navigate up
// that hierarchy.
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* Shows the simplified settings UI if the device configuration if the
* device configuration dictates that a simplified, single-pane UI should be
* shown.
*/
/**
* {@inheritDoc}
*/
@Override
public boolean onIsMultiPane() {
return isXLargeTablet(this) && !isSimplePreferences(this);
}
/**
* Helper method to determine if the device has an extra-large screen. For
* example, 10" tablets are extra-large.
*/
private static boolean isXLargeTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
}
/**
* Determines whether the simplified settings UI should be shown. This is
* true if this is forced via {@link #ALWAYS_SIMPLE_PREFS}, or the device
* doesn't have newer APIs like {@link PreferenceFragment}, or the device
* doesn't have an extra-large screen. In these cases, a single-pane
* "simplified" settings UI should be shown.
*/
private static boolean isSimplePreferences(Context context) {
return ALWAYS_SIMPLE_PREFS
|| Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB
|| !isXLargeTablet(context);
}
/**
* A preference value change listener that updates the preference's summary
* to reflect its new value.
*/
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference.setSummary(
index >= 0
? listPreference.getEntries()[index]
: null);
} else {
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
}
return true;
}
};
/**
* Binds a preference's summary to its value. More specifically, when the
* preference's value is changed, its summary (line of text below the
* preference title) is updated to reflect the value. The summary is also
* immediately updated upon calling this method. The exact display format is
* dependent on the type of preference.
*
* @see #sBindPreferenceSummaryToValueListener
*/
private static void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
// Trigger the listener immediately with the preference's
// current value.
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}
}
首pref_general.xml文件:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/pref_header_personalizacja">
<ListPreference
android:key="grupa_jezykowa_list"
android:title="@string/pref_title_grupa_jezykowa"
android:defaultValue="1"
android:entries="@array/pref_grupa_jezykowa_list_titles"
android:entryValues="@array/pref_grupa_jezykowa_values"
android:negativeButtonText="@null"
android:positiveButtonText="@null"/>
<ListPreference
android:key="grupa_angielski_list"
android:title="@string/pref_title_grupa_angielski"
android:defaultValue="1"
android:entries="@array/pref_grupa_angielski_list_titles"
android:entryValues="@array/pref_grupa_angielski_values"
android:negativeButtonText="@null"
android:positiveButtonText="@null"/>
<ListPreference
android:key="grupa_informatyka_list"
android:title="@string/pref_title_grupa_informatyka"
android:defaultValue="1"
android:entries="@array/pref_grupa_informatyka_list_titles"
android:entryValues="@array/pref_grupa_informatyka_values"
android:negativeButtonText="@null"
android:positiveButtonText="@null"/>
<CheckBoxPreference
android:key="wdz_checkbox"
android:title="@string/pref_title_wdz"
android:summary="@string/pref_description_wdz"
android:defaultValue="false"/>
</PreferenceCategory>
這裏是仿真器的開放應用首次進入設置後的logcat然後回去:
https://dl.dropboxusercontent.com/u/6200994/logcat.txt
忘記了最重要的東西...其中來自logcat的日誌?而另一件事...存儲在代碼中的數據?你想如何迅速改變? – Selvin
你有logcat的? –
謝謝提醒。 – Konpon96