Reputation: 33
I only have a Window where you push menu -> preferences and launch an Intent
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_settings:
startActivity(new Intent(this, com.example.app.PreferenciasGenerales.class));
return true;
default:
return false;
}
}
Automatically it says that the application has to be closed.
Uncaught handler: thread main exiting due to uncaught exception
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.PreferenciasGenerales}:
java.lang.ClassCastException: java.lang.Integer
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
at android.app.ActivityThread.access$2200(ActivityThread.java:119)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: java.lang.Integer at android.app.ApplicationContext$SharedPreferencesImpl.getString(ApplicationContext.java:2572) at android.preference.Preference.getPersistedString(Preference.java:1250) at android.preference.ListPreference.onSetInitialValue(ListPreference.java:232) at android.preference.Preference.dispatchSetInitialValue(Preference.java:1173) at android.preference.Preference.onAttachedToHierarchy(Preference.java:985) at android.preference.PreferenceGroup.addPreference(PreferenceGroup.java:156) at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:97) at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:38) at android.preference.GenericInflater.rInflate(GenericInflater.java:488) at android.preference.GenericInflater.rInflate(GenericInflater.java:493) at android.preference.GenericInflater.inflate(GenericInflater.java:326) at android.preference.GenericInflater.inflate(GenericInflater.java:263) at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:254) at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:253) -> at com.example.app.PreferenciasGenerales.onCreate(PreferenciasGenerales.java:34) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) ... 11 more
The PreferenceActivity that I want to launch is this:
public class PreferenciasGenerales extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*->*/addPreferencesFromResource(R.xml.preferencias_general);/*<- Here is the exception at StackTrace */
}
@Override
protected void onStop() {
final ProgressDialog waitDialog =
ProgressDialog.show( this, "Wait", "i'm testing that those things you write are correct...", true);
waitDialog.setCancelable(false);
/* Cannot have integers array on ListPreference
* http://code.google.com/p/android/issues/detail?id=2096 */
String old_debug_level = PreferenceManager.getDefaultSharedPreferences(this).getString("debug_level", "3");
PreferenceManager.getDefaultSharedPreferences(this).edit()
.remove("debug_level")
.putInt("debug_level", Integer.parseInt(old_debug_level))
.commit();
old_debug_level = null;
String api_server = PreferenceManager.getDefaultSharedPreferences(PreferenciasGenerales.this).getString("api_server", "");
String account_id = PreferenceManager.getDefaultSharedPreferences(PreferenciasGenerales.this).getString("account_id", "");
String account_pass = PreferenceManager.getDefaultSharedPreferences(PreferenciasGenerales.this).getString("account_pass", "");
String account_key = PreferenceManager.getDefaultSharedPreferences(PreferenciasGenerales.this).getString("account_key", "");
boolean debug_enabled= PreferenceManager.getDefaultSharedPreferences(PreferenciasGenerales.this).getBoolean("debug_enabled", false);
int debug_level = PreferenceManager.getDefaultSharedPreferences(PreferenciasGenerales.this).getInt("debug_level", 3);
try {
// Connect to API to test credentials...
More things that you'll need that are related to PreferenceActivity... I've removed all strings.xml values because they are too many and are in Spanish. All strings like title, hint, dialogTitle exists and are correct. They work on other Activities
/res/xml/preferencias_general.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory>
<EditTextPreference android:key="api_server"/>
</PreferenceCategory>
<PreferenceCategory android:title="@string/preferencias_cuenta_categoria_title">
<EditTextPreference android:key="account_id" />
<EditTextPreference android:key="account_pass" />
<EditTextPreference android:key="account_key" />
</PreferenceCategory>
<PreferenceCategory android:key="debug_enabled" />
<ListPreference android:key="debug_level" android:entryValues="@array/debug_level_entryValues" android:entries="@array/debug_level_entries" />
</PreferenceCategory>
/res/values/preferencias_general.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="debug_level_entries">
<item>VERBOSE</item>
<item>DEBUG</item>
<item>INFO</item>
<item>WARN</item>
<item>ERROR</item>
</string-array>
<string-array name="debug_level_entryValues">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
</string-array>
--
UPDATE 1 On answer to @CommonsWere I wiped data out, and it shows it incomplete:
Before that, I try to exit and a new Exception raises up:
java.lang.RuntimeException: Unable to stop activity {com.merinosa.kimera/com.merinosa.kimera.PreferenciasGenerales}: java.lang.IllegalStateException: Target host must not be null, or set in parameters. at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3421) at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3487) at android.app.ActivityThread.access$2800(ActivityThread.java:119) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1896) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:4363) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:521) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:561) at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:292) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) at com.example.app.PreferenciasGenerales.onStop(PreferenciasGenerales.java:78) at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1169) at android.app.Activity.performStop(Activity.java:3797) at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3416) ... 11 more
Where should I test if configurations are correct or not then? If I run app again it goes to first error again.
--
UPDATE 2
My new error has nothing to do with my original question so it can be set as answered.
The reason that the error reappears is because I modify settings as CommonsWare says on the accepted answer.
When I exit from setting it executes onStop() and modify SharedPreferences to integer making it "uncasteable".
New errors has to be properly investigated and asked again if needed.
Upvotes: 0
Views: 1580
Reputation: 1006744
This app, on whatever device or emulator you are using for testing, already has saved SharedPreferences
, where a saved value is an Integer
, and you are now trying to load it into a Preference
that expects a String
. Try clearing the app's data via Settings, or uninstalling and reinstalling the app, to clear up this problem.
Also, please note that in your current preference XML, debug_level
will be saved as a String
, but you are attempting to retrieve it via getInt()
.
Upvotes: 3