gpt4 book ai didi

java - EditTextPreference 不适用于字符串

转载 作者:行者123 更新时间:2023-12-01 18:36:49 27 4
gpt4 key购买 nike

我使用 Android 模板创建了一个设置 Activity 。首先,一切正常,但现在我想从 EditTextPreference 获取 String。但是当我开始运行它时,它因以下错误而崩溃:

java.lang.ClassCastException: java.lang.Boolean cannot be cast to 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>

主要 Activity :

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

设置 Activity :

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

最佳答案

根据评论:

您之前保存的数据可能会破坏当前的实现。如果是这种情况,清洁存储应该可以做到

关于java - EditTextPreference 不适用于字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60019607/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com