2011-02-19 84 views
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(); 

和偏好活動我已經計數定義爲我之前展示,還試圖把東西在字符串中但仍然是相同的結果

+0

試着做不是空字符串以外的字符串COUNT東西。我不知道爲什麼它會給你一個類演員例外,但我覺得這可能是你的問題。 – FoamyGuy 2011-02-19 06:21:39

回答

4

您在COUNT的偏好設置中存儲什麼?我想你可能已經將一些int值存儲爲一個字符串。

就像我覺得有這樣的事情在你的代碼:

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

,後來你在做一個prefs.getInt(COUNT),因爲它是做,因爲LAST和COUNT決心的應該失敗同樣的鑰匙。

+0

不,我不使用putstring()只有putint()。我把我的代碼放在了我的第一篇文章 – tyczj 2011-02-19 22:30:29

+0

中,我也評論說getInt()只是爲了確保問題和程序運行正常,所以我不知道問題是什麼 – tyczj 2011-02-20 05:20:12

3

SharedPreferences.getInt()文檔說:

拋出ClassCastException - 如果有 這個名稱的偏好是 不是int。

因此,要確定它是什麼:

將到getAll通話SharedPreferences pref = Preferenc... 這樣後:

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

連接與調試您的應用程序,將斷點上標線並查看調試器在您的COUNT密鑰的allPrefs映射中顯示的內容。

0

我通過從SharedPreferences聲明字段中刪除private來解決此問題。無論你存儲的價值,但在這種情況下,你應該轉換int值的字符串或者以上

0

提那些答案應該是這樣使用:()

整數,後面跟着的ToString。它將允許將任何整數值分配給字符串。

例如:

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

您將得到類轉換異常,所以你必須存儲或轉換的字符串,然後值的setText

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

對於一些老機器人(< = 4)getBoolean,getInt,...的getString拋出ClassCastException - 如果你從來沒有初始化,並保存變量。

這就是說,解決的辦法是:

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