• 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
2 answers 85 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 822 views
Problem : I want to input some of the details about student to my program. But when I press a save button its giving me the below listed exception error: "Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet."
asked Jan 31, 2020 jwilliam 3.9k points
0 votes
2 answers 274 views
Problem : I got bellow message from my code Exception in thread "main" java.lang.ArithmeticException: / by zero
asked Nov 1, 2019 peterlaw 6.9k points
0 votes
2 answers 199 views
Problem : I am new to string handling & getting below error while trying to execute a program java.lang.stringindexoutofboundsexception: string index out of range: 0
asked Oct 23, 2019 peterlaw 6.9k points
0 votes
1 answer 363 views
Problem : Currently I am trying to display a JSON content using a API key. But I am unable to get a authentication. I am facing below error in my JsonObject: &ldquo;Org.json.JSONException: Value Authorization of type java.lang.String cannot be converted to JSONObject&rdquo; In my ... to pass a API key and URL id to get my JSON response in a URL. I display a JSON content using a JSON array.
asked Feb 2, 2020 jwilliam 3.9k points
0 votes
1 answer 6 views
Solve this problem, please.
asked Dec 30, 2020 TeamScript 9.2k points
0 votes
1 answer 370 views
Problem : I am getting bellow error while using Sockets java.net.socketexception: software caused connection abort: socket write error
asked Oct 19, 2019 peterlaw 6.9k points
0 votes
1 answer 655 views
Problem : I am trying to learn Android programming also I am trying to develop a very basic weather app. My code works perfectly if I try to enter any valid city name. But I want to use the toast and it shows me the error and also to the ... .nextCleanInternal(JSONTokener.java:116) at org.json.JSONTokener.nextValue(JSONTokener.java:94) at org.json.JSONObject. How can I fix the above error?
asked Feb 14, 2020 mphil 2.3k points
0 votes
1 answer 18 views
I need to find frequency of charts in a string? can anyone here help me?
asked Sep 22, 2020 Daniel Anderson 4k points
0 votes
1 answer 127 views
Cannot create controller in thread that did not call Looper.prepare () What does the following exception mean? How can I fix it? Here is the code: Toast toast = Toast.makeText (mContext, "Something", Toast.LENGTH_SHORT); This is the exception: Java.lang.RuntimeException: Cannot ... :121)      on Android.widget.Toast. <init> (Toast.Java:68)      on Android.widget.Toast.makeText (Toast.Java:231)
asked Aug 17, 2020 game 4.6k points