简体   繁体   中英

Strange ClassCastException from Double to Integer on android

I build a android widget, and in the WidgetService class which extends BroadcastReceiver.

A vary strange thing happens, when I try to get an integer from an ArrayList, it throws an exception

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

I have check if it was caused by NullPointer though, no clue.

Is it the problem with my jdk (version 1.8.0_74) or android studio ?(version 1.51) Android SDK Version: 6.0(but I have tried 4.4, still the same)

I try to start a new project and copy to it, but still not working.

How is it possible? Since "staredIndex" is an ArrayList with Integer paramater and randomIndex is an int, currentIndex is and int.

Where is "Double" from?

How to solve it?

Error Message

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) 

Thanks everyone. The problem I found is here. The code below initialize the 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!
        }
        ...

Then I change to this, and the problem is completely solved.

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

Use

Double d = staredIndex.get(randomIndex);
currentIndex = d.intValue();

Or

currentIndex = (int) staredIndex.get(randomIndex);

If you want to cast from double to int .

问题来了,因为ArrayList staredIndex包含不同类型的数据.ArrayList包含Double和Integer类型的数据,但是您已将staredIndex ArrayList声明为Integer。您应该只在ArrayList中添加整数类型的数据,否则应该将类型从Double转换为整数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM