当我尝试访问多维数组时,应用程序崩溃

时间:2013-05-23 12:39:49

标签: java android arrays

出于某种神秘的原因,以下代码使我的Android应用程序崩溃:

public class NearestFixMarker {

private static MyArrayListMarker nearestFixMarker[] = new MyArrayListMarker[Configuration.Display.getNumberOfFixType()];

private static Bitmap icon[][] = new Bitmap[Configuration.Display.getNumberOfFixType()][GenericNdData.getNearestFixList().getNearestFixCount()];

public static void add(int color) {
    for (int i = 0; i < icon.length; i++) {
        Drawable d = DrawableUtils.resizeImageToDrawable(
                MapViewFragment.mapViewActivity,
                Configuration.Display.getDrawableFix(i),
                Configuration.MapView.getWaypointIconWidth(),
                Configuration.MapView.getWaypointIconHeight());
        d.setColorFilter(color, Mode.MULTIPLY);

        Bitmap b = ((BitmapDrawable) d).getBitmap();

        Paint pnt = new Paint();
        Canvas myCanvas = new Canvas(b);

        int myColor = b.getPixel(0,0);

        ColorFilter filter = new LightingColorFilter(myColor, color);
        pnt.setColorFilter(filter);

        myCanvas.drawBitmap(b,0,0,pnt);

        icon[i][0] = b;
        nearestFixMarker[i] = new MyArrayListMarker();
    }
}
    ...
}

这是Logcat显示的内容:

05-23 14:24:42.918: W/dalvikvm(3660): threadid=1: thread exiting with uncaught exception (group=0x41c83930)
05-23 14:24:42.928: E/AndroidRuntime(3660): FATAL EXCEPTION: main
05-23 14:24:42.928: E/AndroidRuntime(3660): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ihm/com.example.ihm.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
05-23 14:24:42.928: E/AndroidRuntime(3660):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-23 14:24:42.928: E/AndroidRuntime(3660):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-23 14:24:42.928: E/AndroidRuntime(3660):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-23 14:24:42.928: E/AndroidRuntime(3660):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-23 14:24:42.928: E/AndroidRuntime(3660):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 14:24:42.928: E/AndroidRuntime(3660):     at android.os.Looper.loop(Looper.java:137)
05-23 14:24:42.928: E/AndroidRuntime(3660):     at android.app.ActivityThread.main(ActivityThread.java:5039)
05-23 14:24:42.928: E/AndroidRuntime(3660):     at java.lang.reflect.Method.invokeNative(Native Method)
05-23 14:24:42.928: E/AndroidRuntime(3660):     at java.lang.reflect.Method.invoke(Method.java:511)
05-23 14:24:42.928: E/AndroidRuntime(3660):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-23 14:24:42.928: E/AndroidRuntime(3660):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-23 14:24:42.928: E/AndroidRuntime(3660):     at dalvik.system.NativeStart.main(Native Method)
05-23 14:24:42.928: E/AndroidRuntime(3660): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
05-23 14:24:42.928: E/AndroidRuntime(3660):     at com.example.ihm.mapview.marker.NearestFixMarker.add(NearestFixMarker.java:73)
05-23 14:24:42.928: E/AndroidRuntime(3660):     at com.example.ihm.mapview.MapViewFragment.onActivityCreated(MapViewFragment.java:151)
05-23 14:24:42.928: E/AndroidRuntime(3660):     at android.app.Fragment.performActivityCreated(Fragment.java:1703)
05-23 14:24:42.928: E/AndroidRuntime(3660):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
05-23 14:24:42.928: E/AndroidRuntime(3660):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
05-23 14:24:42.928: E/AndroidRuntime(3660):     at android.app.BackStackRecord.run(BackStackRecord.java:682)
05-23 14:24:42.928: E/AndroidRuntime(3660):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
05-23 14:24:42.928: E/AndroidRuntime(3660):     at android.app.Activity.performStart(Activity.java:5113)
05-23 14:24:42.928: E/AndroidRuntime(3660):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
05-23 14:24:42.928: E/AndroidRuntime(3660):     ... 11 more
05-23 14:24:42.928: W/ActivityManager(497):   Force finishing activity com.example.ihm/.MainActivity

显然,它正在访问造成问题的阵列,但我不明白为什么会这样。我之前有一个简单的数组,它工作得很好。现在,我用一个多维数组替换它,它崩溃了。我错过了一些明显的东西吗?

1 个答案:

答案 0 :(得分:1)

你在这一行得到例外

icon[i][0] = b;

此数组的大小为0,因此您可以访问0元素

Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0