我用安卓模板创建了一个设置活动。 首先,一切正常,但现在我想从EditTextPreference
那里得到String
.但是当我开始运行它时,由于此错误,它崩溃了:
java.lang.ClassCastException: java.lang.Boolean 不能强制转换为 java.lang.String
我使用了正确的Key和getString方法。 由于每次我想打开"设置"时都会发生这种情况,因此我的应用程序崩溃并且出现该错误:
2020-02-01 17:22:55.260 24468-24468/de.fabipfolix.vertretungsplan E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.fabipfolix.vertretungsplan, PID: 24468
java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:288)
at androidx.preference.Preference.getPersistedString(Preference.java:1686)
at androidx.preference.EditTextPreference.onSetInitialValue(EditTextPreference.java:106)
at androidx.preference.Preference.onSetInitialValue(Preference.java:1614)
at androidx.preference.Preference.dispatchSetInitialValue(Preference.java:1587)
at androidx.preference.Preference.onAttachedToHierarchy(Preference.java:1311)
at androidx.preference.Preference.onAttachedToHierarchy(Preference.java:1326)
at androidx.preference.PreferenceGroup.addPreference(PreferenceGroup.java:249)
at androidx.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:170)
at androidx.preference.PreferenceInflater.rInflate(PreferenceInflater.java:345)
at androidx.preference.PreferenceInflater.rInflate(PreferenceInflater.java:346)
at androidx.preference.PreferenceInflater.inflate(PreferenceInflater.java:157)
at androidx.preference.PreferenceInflater.inflate(PreferenceInflater.java:109)
at androidx.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:216)
at androidx.preference.PreferenceFragmentCompat.setPreferencesFromResource(PreferenceFragmentCompat.java:377)
at de.fabipfolix.vertretungsplan.SettingsActivity$SettingsFragment.onCreatePreferences(SettingsActivity.java:42)
at androidx.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:160)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2586)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:838)
at androidx.fragment.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1197)
at androidx.fragment.app.FragmentTransition.calculateFragments(FragmentTransition.java:1080)
at androidx.fragment.app.FragmentTransition.startTransitions(FragmentTransition.java:119)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1866)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2663)
at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2613)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:246)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:542)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:201)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1425)
at android.app.Activity.performStart(Activity.java:7825)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3294)
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
我已经评论了我今天所做的一切,我不知道问题是什么。 XML文件的设计窗口说我必须放一个boolean
。 有谁知道如何解决这个问题?
这是我的文件:
root_preferences.xml:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory app:title="Login Daten">
<EditTextPreference
android:ems="10"
android:inputType="text"
android:key="pref_username"
android:title="BenutzernameEditText"
app:title="Benutzername"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
android:dialogTitle="Passwort"
android:inputType="text"
android:key="pref_password"
android:title="Passwort" />
</PreferenceCategory>
<PreferenceCategory app:title="Benachrichtigungen">
<SwitchPreference
android:defaultValue="true"
android:key="switchNotifications"
android:summary="@string/summarySwitchNotifications"
android:title="NotificationSwitch" />
</PreferenceCategory>
<PreferenceCategory app:title="Hintergrundaktivität">
</PreferenceCategory>
<PreferenceCategory app:title="Oberstufe">
<SwitchPreference
android:contentDescription="Kursbezeichnungen mit Komma trennen"
android:defaultValue="false"
android:key="switchKurse"
android:title="Kurse filtern" />
<EditTextPreference
android:dialogMessage="Kursnamen mit Komma trennen "
android:key="pref_kurse"
android:summary="Kursnamen mit Komma trennen "
android:title="Kursbezeichnungen"
android:inputType="text"
app:dialogTitle="Kursbezeichnungen" />
</PreferenceCategory>
主要活动:
public class MainActivity extends AppCompatActivity {
FloatingActionButton fabRefresh;
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
ArrayList<Vertretungsstunde> vertretungen;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences sharedPref =PreferenceManager.getDefaultSharedPreferences(this);
Boolean notifications = sharedPref.getBoolean(SettingsActivity.KEY_PREF_NOTIFICATIONS_SWITCH, false);
String username = sharedPref.getString(SettingsActivity.KEY_PREF_USERNAME_TEXT, "");
ArrayList<Vertretungsstunde> list = ...;
//RECYCLERVIEW
recyclerView = findViewById(R.id.rv_VertretungenHeute);
recyclerView.setHasFixedSize(false);
layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
adapter = new VertretungsAdapter(this, list);
recyclerView.setAdapter(adapter);
//TOOLBAR
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//REFRESH BUTTON
fabRefresh = findViewById(R.id.fabRefresh);
fabRefresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { //FABRefresh OnClickListener
Toast.makeText(MainActivity.this, "Refreshing...", Toast.LENGTH_SHORT).show();
//...
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent intent = new Intent(MainActivity.this, de.fabipfolix.vertretungsplan.SettingsActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
设置活动:
public class SettingsActivity extends AppCompatActivity {
public static final String KEY_PREF_NOTIFICATIONS_SWITCH = "switchNotifications";
public static final String KEY_PREF_FILTER_SWITCH = "switchKurse";
public static final String KEY_PREF_KURSE_TEXT ="pref_kurse";
public static final String KEY_PREF_PASSWORD_TEXT = "pref_password";
public static final String KEY_PREF_USERNAME_TEXT = "pref_username";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.commit();
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
}
}
根据注释:
您可能以前保存了可能破坏当前实现的数据。如果是这种情况,应该清理存储