• 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?

8 4 2
2,300 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.

5 2 1
4,980 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


10 6 4
31,120 points

Related questions

0 votes
1 answer 24 views
0 votes
1 answer 2 views
Problem: Please help me &hellip; how can I solve it? java lang classcastexception cannot be cast to
asked Apr 4 Ifra 24.4k points
0 votes
1 answer 40 views
Problem: java.lang.classcastexception: java.math.biginteger cannot be cast to java.lang.long
asked Feb 24 Hira 2.2k points
1 vote
1 answer 6 views
Problem : Getting error in sending arraylist of object from adapter to other activity. I wanna send my arraylist from onBindViewHolder method of Adapter to another activity but its is showing null exception error on the Cast Class in writetoParcel Method. Getting Error ... Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference How to send arraylist properly?
asked Mar 31 Shovo210 13.2k points
0 votes
1 answer 20 views
Can someone please help me? I would most appreciate it. Thank you very much in advance
asked Jan 23 TeamScript 13.5k points
0 votes
2 answers 122 views
Problem: I am new in programming. I was doing my java practice. I wrote a code: Object imObject = new String(); Integer i = (Integer) imObject; This code gives me the following error: Exception in thread "main" java.lang.ClassCastException: java.lang ... .main(ClassCastExcetpionTest.java:31) I am newbee so I am confused about what is happening , Can anyone explain why is this happening?
asked Mar 23, 2020 LizzyM 6.1k points
0 votes
1 answer 14 views
Problem: Hello! I am trying to run the following code on my PC: #include<stdio.h> void main() { int number_1,number_2,result; char *operation; printf("Enter the first number: "); scanf("%d",&number_1); printf("Enter the second number: "); scanf("%d",&number_2) ... to know why is it giving these warnings? Moreover, what I can do to remove these warnings? Do they affect the working of my program?
asked Mar 17 Code Learner 9.2k points
0 votes
1 answer 34 views
Problem: Any ideas on what I need to do to fix this? Thanks!
asked Mar 15 ummesalma 21.9k points
0 votes
1 answer 9 views
Problem: public boolean clearSelection() { int i = 0; if (!this.m_SelectedComps.isEmpty()) { i = 1; Iterator localIterator = this.m_SelectedComps.iterator(); while (localIterator.hasNext()) ((AnnotComponent) localIterator.next()).remove(); this.m_SelectedComps.clear(); } return i; } Please HELP! How to convert the integer to boolean?
asked Mar 5 neeraj 9.5k points
0 votes
1 answer 7 views
Problem: Any ideas on what I need to fix the error: could not find or load main class main caused by: java.lang.classnotfoundexception: main?
asked Mar 27 ashik 10.6k points