• Register
0 votes

Problem :

Currently I am working on my app for  Android but I am getting following error while working with the SharedPreferences. I am very new to Android App Programming. I am trying to debug it from past 2 days but unable to find the solution on the below error.

E/AndroidRuntime: FATAL EXCEPTION: main
   Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
     at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:235)
     at de.immozukunft.quicksteuer.QuickSteuerActivity.updateTextView(QuickSteuerActivity.java:56)
     at de.immozukunft.quicksteuer.QuickSteuerActivity.onCreate(QuickSteuerActivity.java:36)

Please find below my preference in my XML-File:

android:title="@string/capitalownership" />

Can somebody help me in resolving this error?

2.3k points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

I saw your question on this forum. Many beginners in Android App programming face such issues. I will try to help you in this issue.

The most important thing is your Shared Preferences are stored as the Map<String, Object>. As you can see the second parameters value of a map is the of the Object type, if you have saved it as the Integer then you must retrieve it as the integer. You can see this is of dynamic nature, so if you have saved at one time as the integer and in the the next attempt as the String then you must remember to keep track of a type that you have saved to be able to retrieve it. whenever you want to retrieve it then you must use the getString().

And If you don’t know what you want to retrieve then you can use a getAll() method it will help you in retrieving your entire preferences as a Map<String, Object>

This will help you in resolving your issue.

5k points
0 votes


java.util.List has two overloaded remove systems: one takes a int (index) and returns the removed element, the other one takes an Object and returns a boolean (true in case the list seized the specified element, else false).

You code invites the remove(Object) process, and you're attempting to include its boolean return value to your list: list.add(j, list.remove(current));

Here is the Javadoc for the two overloaded systems:

 * Removes the element at the specified position in this list (optional
 * operation).  Shifts any subsequent elements to the left (subtracts one
 * from their indices).  Returns the element that was removed from the
 * list.
 * @param index the index of the element to be removed
 * @return the element previously at the specified position
 * @throws UnsupportedOperationException if the <tt>remove</tt> operation
 *         is not supported by this list
 * @throws IndexOutOfBoundsException if the index is out of range
 *         (<tt>index &lt; 0 || index &gt;= size()</tt>)
E remove(int index);


 * Removes the first occurrence of the specified element from this list,
 * if it is present (optional operation).  If this list does not contain
 * the element, it is unchanged.  More formally, removes the element with
 * the lowest index <tt>i</tt> such that
 * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>
 * (if such an element exists).  Returns <tt>true</tt> if this list
 * contained the specified element (or equivalently, if this list changed
 * as a result of the call).
 * @param o element to be removed from this list, if present
 * @return <tt>true</tt> if this list contained the specified element
 * @throws ClassCastException if the type of the specified element
 *         is incompatible with this list (optional)
 * @throws NullPointerException if the specified element is null and this
 *         list does not permit null elements (optional)
 * @throws UnsupportedOperationException if the <tt>remove</tt> operation
 *         is not supported by this list
boolean remove(Object o);

This error was happened since you previously include some boolean value to this "key". Attempt to trace some putBoolean in your code and check keys. Further attempt to reinstall current app to solve corrupted preferences.

To solve it, convert it back to the original Integer and cast it to Long

obj.setID(((Integer) row.get("ID")).longValue());
//obj.setID(((Long) row.get("ID")));

Java basic:

Integer num = 1;

Long numInLong = num.longValue();	 // Integer to Long

Long numInLong2 = Long.valueOf(num); // Integer to Long


31.7k points
edited by