Android

时间:2016-02-21 13:54:24

标签: java android classcastexception

我构建了一个android小部件,并在WidgetService类中扩展了BroadcastReceiver。

一个奇怪的事情发生了,当我尝试从ArrayList获取一个整数时,它会抛出一个异常

public class WidgetService extends BroadcastReceiver {
      int currentIndex = -1;
      ArrayList<Integer> indexHitTimes = null;
      ArrayList<Integer> staredIndex = null;
      HashMap<Integer, Integer> staredIndexHitTimes = null;
      ...
      public void onReceive(Context context, Intent intent) {
            ...
            updateCurrentIndex();
            ...
      }
      private void updateCurrentIndex(){
            ...
            Random r = new Random();
            int size = staredIndex.size();
            int randomIndex = r.nextInt(size);
            currentIndex = staredIndex.get(randomIndex);    ->problem here
            currentIndexHitTimes = staredIndexHitTimes.get(currentIndex);
            minHitTimes = Collections.min(staredIndexHitTimes.values());
            ...
      }
}

我检查它是否是由NullPointer引起的,但没有任何线索。

是我的jdk(版本1.8.0_74)还是android studio的问题?(版本1.51) Android SDK版本:6.0(但我尝试过4.4,仍然相同)

我尝试启动一个新项目并复制到它,但仍无效。

怎么可能? 由于“staredIndex”是一个带有Integer参数的ArrayList,randomIndex是一个int,因此currentIndex是和int。

“Double”来自哪里?

如何解决?

错误消息

java.lang.RuntimeException: Unable to start receiver 
heart.david.jp50_reviewer.WidgetService: java.lang.ClassCastException: 
java.lang.Double cannot be cast to java.lang.Integer
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2602)
at android.app.ActivityThread.access$1700(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1358)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Integer
at heart.david.jp50_reviewer.WidgetService.updateCurrentIndex(WidgetService.java:136)
at heart.david.jp50_reviewer.WidgetService.onReceive(WidgetService.java:51)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2595)
at android.app.ActivityThread.access$1700(ActivityThread.java:147) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1358) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5253) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
  

谢谢大家。我发现的问题就在这里。下面的代码初始化了ArrayList。

        ...
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
        String json = sharedPreferences.getString("staredIndex", null);
        if(json == null){
            staredIndex = new ArrayList<Integer>();
            for(int i=0;i<hashMap_Characters.size();i++){
                boolean stared = hashMap_Stared.get(i);
                if(stared){
                    staredIndex.add(i);
                }
            }
            SharedPreferences.Editor editor = sharedPreferences.edit();
            Gson gson = new Gson();
            String staredIndex_json = gson.toJson(staredIndex);
            editor.putString("staredIndex", staredIndex_json);
            editor.commit();
        }else{
            Gson gson = new Gson();
            staredIndex = gson.fromJson(json, ArrayList.class); -> here! the staredIndex filled with Doubles!
        }
        ...
  

然后我改为这个,问题就完全解决了。

        ...
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
        String json = sharedPreferences.getString("indexHitTimes", null);
        if(json == null){
            indexHitTimes = new ArrayList<Integer>();
            for(int i=0;i<hashMap_Characters.size();i++){
                indexHitTimes.add(0);
            }
            SharedPreferences.Editor editor = sharedPreferences.edit();
            Gson gson = new Gson();
            Type type = new TypeToken<ArrayList<Integer>>(){}.getType();
            String indexHitTimes_json = gson.toJson(indexHitTimes, type);
            editor.putString("indexHitTimes", indexHitTimes_json);
            editor.commit();
        }else{
            Gson gson = new Gson();
            indexHitTimes = (ArrayList<Integer>)gson.fromJson(json, new TypeToken<ArrayList<Integer>>(){}.getType());
        }
        ...

0 个答案:

没有答案