2 blank557 Blank557 于 2016.02.18 19:47 提问

Android TimeZone long[]转换成int[]问题

androidM移植,发现日历崩溃,抓log如下:

 02-17 16:41:29.806  4921  4921 D AndroidRuntime: Shutting down VM
02-17 16:41:29.808  4921  4921 E AndroidRuntime: FATAL EXCEPTION: main
02-17 16:41:29.808  4921  4921 E AndroidRuntime: Process: com.android.calendar, PID: 4921
02-17 16:41:29.808  4921  4921 E AndroidRuntime: java.lang.ClassCastException: long[] cannot be cast to int[]
02-17 16:41:29.808  4921  4921 E AndroidRuntime:    at com.android.timezonepicker.TimeZoneInfo.getTransitions(TimeZoneInfo.java:202)
02-17 16:41:29.808  4921  4921 E AndroidRuntime:    at com.android.timezonepicker.TimeZoneInfo.<init>(TimeZoneInfo.java:64)
02-17 16:41:29.808  4921  4921 E AndroidRuntime:    at com.android.timezonepicker.TimeZoneData.loadTzsInZoneTab(TimeZoneData.java:446)
02-17 16:41:29.808  4921  4921 E AndroidRuntime:    at com.android.timezonepicker.TimeZoneData.loadTzs(TimeZoneData.java:120)
02-17 16:41:29.808  4921  4921 E AndroidRuntime:    at com.android.timezonepicker.TimeZoneData.<init>(TimeZoneData.java:80)
02-17 16:41:29.808  4921  4921 E AndroidRuntime:    at com.android.timezonepicker.TimeZonePickerView.<init>(TimeZonePickerView.java:66)
02-17 16:41:29.808  4921  4921 E AndroidRuntime:    at com.android.timezonepicker.TimeZonePickerDialog.onCreateView(TimeZonePickerDialog.java:72)
02-17 16:41:29.808  4921  4921 E AndroidRuntime:    at android.app.Fragment.performCreateView(Fragment.java:2220)
02-17 16:41:29.808  4921  4921 E AndroidRuntime:    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
02-17 16:41:29.808  4921  4921 E AndroidRuntime:    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
02-17 16:41:29.808  4921  4921 E AndroidRuntime:    at android.app.BackStackRecord.run(BackStackRecord.java:793)
02-17 16:41:29.808  4921  4921 E AndroidRuntime:    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
02-17 16:41:29.808  4921  4921 E AndroidRuntime:    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:482)
02-17 16:41:29.808  4921  4921 E AndroidRuntime:    at android.os.Handler.handleCallback(Handler.java:739)
02-17 16:41:29.808  4921  4921 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:95)
02-17 16:41:29.808  4921  4921 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:148)
02-17 16:41:29.808  4921  4921 E AndroidRuntime:    at android.app.ActivityThread.main(ActivityThread.java:5417)
02-17 16:41:29.808  4921  4921 E AndroidRuntime:    at java.lang.reflect.Method.invoke(Native Method)
02-17 16:41:29.808  4921  4921 E AndroidRuntime:    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
02-17 16:41:29.808  4921  4921 E AndroidRuntime:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-17 16:41:29.811  3226  3353 W ActivityManager:   Force finishing activity com.android.calendar/.CalendarSettingsActivity

TimeZoneInfo.java的202附近代码如下:

     private static int[] getTransitions(TimeZone tz, long time)
            throws IllegalAccessException, NoSuchFieldException {
        Class<?> zoneInfoClass = tz.getClass();
        Field mTransitionsField = zoneInfoClass.getDeclaredField("mTransitions");
        mTransitionsField.setAccessible(true);
//此处出错
        int[] objTransitions = (int[])mTransitionsField.get(tz);
        int[] transitions = null;
        if (objTransitions.length != 0) {
            transitions = new int[NUM_OF_TRANSITIONS];
            int numOfTransitions = 0;
            for (int i = 0; i < objTransitions.length; ++i) {
                if (objTransitions[i] < time) {
                    continue;
                }
                transitions[numOfTransitions++] = objTransitions[i];
                if (numOfTransitions == NUM_OF_TRANSITIONS) {
                    break;
                }
            }
        }
        return transitions;
    }

跪求解决

4个回答

caozhy
caozhy   Ds   Rxr 2016.02.18 22:35

int[] objTransitions = (int[])mTransitionsField.get(tz);
不能这么写,在不考虑精度损失和溢出的情况下,可以这么写:

 long[] arr = mTransitionsField.get(tz);
int[] objTransitions = new int[arr.size()];
for (int i = 0; i < arr.size(); i++)
    objTransitions[i] = (int)arr[i];
fanst_
fanst_   2016.02.18 22:37

int[] objTransitions = (int[])mTransitionsField.get(tz);

“mTransitions”这个属性是long[]型的,不要强转为int[]

long[] objTransitions = (long[])mTransitionsField.get(tz);

luochoudan
luochoudan   Ds   Rxr 2016.02.18 23:10

一个元素一个元素转吧,好像也没更好的方法。

Mr_dsw
Mr_dsw   Ds   Rxr 2016.02.18 20:45

你移植的什么?系统啊!代码在这环境上怎么会有问题啊

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!