我是android的新手,并试图建立一个标志测验。但是当我运行我的程序时,我得到了这个错误:
java.lang.ClassCastException: java.lang.String cannot be cast to
java.util.Set
因为我的MainActivity中的这一行:
Set<String> regions=sharedPreferences.getStringSet(REGIONS,null);
这是我的MainActivity:
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
public static final String CHOICES="pref_numberOfChoices";
public static final String REGIONS="pref_regionsToInclude";
private boolean phoneDevice=true;
private boolean preferencesChanged=true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
int screenSize=getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK;
if(screenSize==Configuration.SCREENLAYOUT_SIZE_LARGE ||
screenSize==Configuration.SCREENLAYOUT_SIZE_XLARGE)
phoneDevice=false;
if(phoneDevice)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
PreferenceManager.setDefaultValues(this,R.xml.preferences,false);
PreferenceManager.getDefaultSharedPreferences(this).
registerOnSharedPreferenceChangeListener(preferencesChangeListener);
SharedPreferences sharedPreferences=(SharedPreferences) PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor=sharedPreferences.edit();
Set<String> regions=sharedPreferences.getStringSet(REGIONS,null);
if (regions.size() == 0) {
regions.add(getString(R.string.default_region));
editor.putStringSet(REGIONS, regions);
editor.apply();
}
}
@Override
protected void onStart()
{
super.onStart();
if(preferencesChanged)
{
MainActivityFragment quizFragment=(MainActivityFragment)
getSupportFragmentManager().findFragmentById(R.id.quizFragment);
quizFragment.updateGuessRows(PreferenceManager.getDefaultSharedPreferences(this));
quizFragment.updateRegions(PreferenceManager.getDefaultSharedPreferences(this));
quizFragment.resetQuiz();
preferencesChanged=false;
}
}
private SharedPreferences.OnSharedPreferenceChangeListener preferencesChangeListener=
new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
preferencesChanged = true;
MainActivityFragment quizFragment = (MainActivityFragment) getSupportFragmentManager().
findFragmentById(R.id.quizFragment);
if (s.equals(REGIONS)) {
Set<String> regions = sharedPreferences.getStringSet(REGIONS, null);
if (regions.size() == 0) {
SharedPreferences.Editor editor = sharedPreferences.edit();
regions.add(getString(R.string.default_region));
editor.putStringSet(REGIONS, regions);
editor.apply();
Toast.makeText(MainActivity.this, R.string.default_region_message, Toast.LENGTH_SHORT).show();
}
}
Toast.makeText(MainActivity.this, R.string.restarting_quiz, Toast.LENGTH_SHORT).show();
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
int orientation=getResources().getConfiguration().orientation;
if(orientation== Configuration.ORIENTATION_PORTRAIT) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
else
return false;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent preferencesIntent=new Intent(this,SettingsActivity.class);
startActivity(preferencesIntent);
return super.onOptionsItemSelected(item);
}}
这是preferences.xml:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:entries="@array/guesses_list"
android:entryValues="@array/guesses_list"
android:key="pref_numberOfChoices"
android:title="@string/number_of_choices"
android:summary="@string/number_of_choices_description"
android:persistent="true"
android:defaultValue="4" />
<MultiSelectListPreference
android:entries="@array/regions_list_for_settings"
android:entryValues="@array/regions_list"
android:key="pref_regionsToInclude"
android:title="@string/world_regions"
android:summary="@string/world_regions_description"
android:persistent="true"
android:defaultValue="@array/regions_list"/>
</PreferenceScreen>
这是arrays.xml:
<resources>
<string-array name="regions_list">
<item>Africa</item>
<item>Asia</item>
<item>Europe</item>
<item>North_America</item>
<item>Oceania</item>
<item>South_America</item>
</string-array>
<string-array name="regions_list_for_settings">
<item>Africa</item>
<item>Asia</item>
<item>Europe</item>
<item>North America</item>
<item>Oceania</item>
<item>South America</item>
</string-array>
<string-array name="guesses_list">
<item>2</item>
<item>4</item>
<item>6</item>
<item>8</item>
</string-array>
有什么建议吗?
答案 0 :(得分:0)
参考文档,您的pref_regionsToInclude
不是Set
的关键。
getStringSet
返回优先级值(如果存在)或defValues。如果存在此名称不是Set的首选项,则抛出ClassCastException。 该值可以为null
查看here了解详情