• Register
0 votes
112 views

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:

<EditTextPreference
android:defaultValue="0"
android:dialogTitle="@string/capitalownership"
android:inputType="number"
android:key="capitalownership"
android:title="@string/capitalownership" />

Can somebody help me in resolving this error?

8 4 2
2,300 points

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

Solution:

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);

and:

/**
 * 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 48 views
48 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 LizzyM 6.1k points
0 votes
1 answer 510 views
510 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 jwilliam 3.9k points
0 votes
2 answers 117 views
117 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 163 views
163 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 224 views
224 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 jwilliam 3.9k points