2

i keep getting a class cast exception error when i run my program and i am not entirely sure why.

error

02-18 14:31:27.585: ERROR/AndroidRuntime(325): FATAL EXCEPTION: main
02-18 14:31:27.585: ERROR/AndroidRuntime(325): java.lang.RuntimeException: Unable to start receiver com.app.notifyme.SmsReciever: java.lang.ClassCastException: java.lang.String
02-18 14:31:27.585: ERROR/AndroidRuntime(325):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2821)
02-18 14:31:27.585: ERROR/AndroidRuntime(325):     at android.app.ActivityThread.access$3200(ActivityThread.java:125)
02-18 14:31:27.585: ERROR/AndroidRuntime(325):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
02-18 14:31:27.585: ERROR/AndroidRuntime(325):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 14:31:27.585: ERROR/AndroidRuntime(325):     at android.os.Looper.loop(Looper.java:123)
02-18 14:31:27.585: ERROR/AndroidRuntime(325):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-18 14:31:27.585: ERROR/AndroidRuntime(325):     at java.lang.reflect.Method.invokeNative(Native Method)
02-18 14:31:27.585: ERROR/AndroidRuntime(325):     at java.lang.reflect.Method.invoke(Method.java:521)
02-18 14:31:27.585: ERROR/AndroidRuntime(325):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-18 14:31:27.585: ERROR/AndroidRuntime(325):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-18 14:31:27.585: ERROR/AndroidRuntime(325):     at dalvik.system.NativeStart.main(Native Method)
02-18 14:31:27.585: ERROR/AndroidRuntime(325): Caused by: java.lang.ClassCastException: java.lang.String
02-18 14:31:27.585: ERROR/AndroidRuntime(325):     at android.app.ContextImpl$SharedPreferencesImpl.getInt(ContextImpl.java:2706)
02-18 14:31:27.585: ERROR/AndroidRuntime(325):     at com.app.notifyme.SmsReciever.onReceive(SmsReciever.java:45)
02-18 14:31:27.585: ERROR/AndroidRuntime(325):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2810)

now if i read that right it is saying the error is at line 45 in SmsReciever which would make this the problem area.

SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
unread = pref.getInt(SmsPrefs.COUNT, 0);

I have everything defined as such

private int unread = 0;
//in preference class
public static final String COUNT = "";

I am just trying to use this variable to keep a count. Someone guide me along here because i really dont see a problem.

update***

how code is

public class SmsReciever extends BroadcastReceiver {

static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";

NotificationManager notifyManag;

String mLast = new String();
private int unread = 0;

@Override
public void onReceive(Context arg0, Intent arg1) {

    boolean smsOn = false;
    String smsColor = new String ("Green");
    Uri smsSound;
    String smsVibrate = new String ("Normal");


    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(arg0);

     smsOn = pref.getBoolean("PREF_SMS_ON", false);
     smsColor = pref.getString("SMS_PREF_COLOR", "Green");
     smsSound = Uri.parse(pref.getString("SMS_PREF_SOUND", "Silent"));
     smsVibrate = pref.getString("SMS_PREF_SOUND", "Normal");
     unread = pref.getInt(SmsPrefs.COUNT, 0);
     mLast = pref.getString(SmsPrefs.LAST, "");

       NotificationManager mNotificationManager = (NotificationManager) arg0.getSystemService(Context.NOTIFICATION_SERVICE);
        if (arg1.getAction().equals(ACTION) && smsOn == true){
            String from = new String();
            String body = new String();

            Bundle bundle = arg1.getExtras();
            if (bundle != null) {
                Object[] pdus = (Object[]) bundle.get("pdus");
                for (Object pdu : pdus){
                SmsMessage messages = SmsMessage.createFromPdu((byte[]) pdu);
                from = messages.getDisplayOriginatingAddress();
                body= messages.getDisplayMessageBody();
                }// end for
            }//end if

            int icon = 0;
            CharSequence tickerText = null;
            long when = 0;
            SharedPreferences.Editor editor = pref.edit();

            icon = icon(icon);
            tickerText = from + ": " + body;
            when = System.currentTimeMillis();
            CharSequence contentText = "";
            CharSequence contentTitle = "";

            /*
             if no notifications do normal
             else if notified >= 1 and last message is from same person display name and how many messages
             else if(notified >=1 and last message is from different display new message and how many
             */
            if(unread == 0){
                contentTitle = from;
                contentText = body.toString();
                unread = 1;
                editor.putInt(SmsPrefs.COUNT, unread);
                editor.commit();
            }else if(unread >= 1 && mLast.equals(from)){
                contentTitle = from;
                contentText = unread + " unread messages";
                unread++;
                editor.putInt(SmsPrefs.COUNT, unread);
                editor.commit();
            }else if(unread >= 1 && !mLast.equals(from)){
                contentTitle = "New Messages";
                contentText = unread + " unread messages";
                unread++;
                editor.putInt(SmsPrefs.COUNT, unread);
                editor.commit();
            }

            mLast = from;
            editor.putString(SmsPrefs.LAST, mLast);
            editor.commit();

and in the preference activity I have count defined as i showed earlier, also tried putting something in the string but still the same result

1
  • Try making the String COUNT something other than an empty string. I don't know why it would be giving you a class cast exception but I feel like this may be your problem. Commented Feb 19, 2011 at 6:21

5 Answers 5

4

What are you storing in the preferences for COUNT? I think you may have stored some int value as a string accidentally.

Like I think there is something like this in your code:

int a = 1;
prefs.putInt(COUNT, a);
String LAST = "";
prefs.putString(LAST, "NAME");
prefs.commit();
...

and later on you are doing a prefs.getInt(COUNT) which should fail as it is doing because LAST and COUNT resolve to the same key.

Sign up to request clarification or add additional context in comments.

5 Comments

no i dont use putstring() at all only putint(). I put in my code where i use that in my first post
also I commented that getInt() line out just to make sure that was the problem and the program ran fine so i dont know what the problem is
Sorry I am on a very slow and nasty connection so the code box never appeared. Anyways what's the value of LAST. if LAST.equals(COUNT) then that is your problem. Make sure COUNT and LAST are unique.
where are you seeing last.equals(count)?? because i never did that. I only do mLast.equals(from)
Can you put the value of LAST in the code as well? Without that we can't tell. I am not saying mLast I am saying your SmsPrefs.COUNT and SmsPrefs.LAST seem to be the same value.
3

SharedPreferences.getInt() docs says:

Throws ClassCastException if there is a preference with this name that is not an int.

So to determine what it is:

Put a call to getAll after SharedPreferences pref = Preferenc... like this:

SharedPreferences pref = Preferenc...
Map<String,?> allPrefs = pref.getAll();
Log.i("xx",allPrefs); // Dummy , put break point here

Connect to your app with the debugger, put a breakpoint on the marked line and see what the debugger shows in the allPrefs map for your COUNT key.

2 Comments

that bit of code gives me an error on the log.i saying arguments are not acceptable. what i did however is take away the private from the int unread variable and the program now works right. does that make sense and is that safe to do?
Makes no sense to me :-/ The log.i should not throw a CCE, but was merely thought as point to put the breakpoint on and to prevent the compiler to optimize the otherwise unused allPrefs variable from being optimized away.
0

I fixed this problem by removing private from the SharedPreferences declaration field.

Comments

0

Wherever you store the value but in that case you should convert int value string or else above mention those answer should be use like this:

integer value followed by .tostring(). it will allow to assign any integer value to string.

For Example:

Textview mtxtname= new Textview(this);
mtxtname.setText(19);

You will got class cast exception so you have to store or convert string and then setText of the value.

String s= 19+"";
mtextname.settext(s.tostring()); 

Comments

0

For some old androids ( <= 4 ) getBoolean, getInt, getString... throw ClassCastException if you never initialized and saved the variable.

That said, the solution is:

type foo;
try{
   foo = sharedPreferences.getANYTING("name", defaultValue);
}
catch( ClassCastException e ){
    foo = defaultValue;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.