2

当我运行我的程序时,我不断收到一个类转换异常错误,我不完全确定为什么。

错误

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)

现在,如果我没看错,就是说错误位于 SmsReciever 的第 45 行,这将使其成为问题区域。

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

我已经定义了一切

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

我只是想用这个变量来保持计数。有人在这里指导我,因为我真的没有看到问题。

更新* **

代码是怎样的

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

并且在我之前显示的定义的偏好活动中,还尝试在字符串中放入一些东西,但结果仍然相同

4

5 回答 5

4

您在 COUNT 的首选项中存储了什么?我认为您可能不小心将一些 int 值存储为字符串。

就像我认为你的代码中有这样的东西:

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

后来你正在做一个 prefs.getInt(COUNT) 应该失败,因为 LAST 和 COUNT 解析为同一个键。

于 2011-02-19T06:29:54.527 回答
3

SharedPreferences.getInt()文档说:

如果此名称的首选项不是 int,则抛出 ClassCastException。

所以要确定它是什么:

像这样打电话给getAllafter SharedPreferences pref = Preferenc...

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

使用调试器连接到您的应用程序,在标记的行上放置一个断点,然后查看调试器在allPrefs映射中为您的COUNT密钥显示的内容。

于 2011-02-20T21:12:13.237 回答
0

我通过privateSharedPreferences声明字段中删除来解决此问题。

于 2013-04-10T00:47:17.477 回答
0

无论您将值存储在哪里,但在这种情况下,您应该转换 int 值字符串,否则上面提到的那些答案应该像这样使用:

整数值后跟 .tostring()。它将允许将任何整数值分配给字符串。

例如:

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

您将得到类转换异常,因此您必须存储或转换字符串,然后setText是值。

String s= 19+"";
mtextname.settext(s.tostring()); 
于 2014-03-21T07:31:52.273 回答
0

对于一些旧机器人 (<= 4) getBoolean, getInt, getString... 如果您从未初始化并保存变量,则抛出 ClassCastException。

也就是说,解决方案是:

type foo;
try{
   foo = sharedPreferences.getANYTING("name", defaultValue);
}
catch( ClassCastException e ){
    foo = defaultValue;
}
于 2017-06-24T13:26:13.807 回答