简体   繁体   English

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

[英]Application crashes when I try to access a multidimensional array

For some mysterious reason, the following code makes my Android application crash : 由于某些神秘的原因,以下代码使我的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();
    }
}
    ...
}

Here is what the Logcat shows : 这是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

Apparently, it's accessing the array that poses a problem, but I can't see why it does. 显然,它正在访问构成问题的数组,但我不知道为什么会这样。 I had a simple array before, and it worked just fine. 我以前有一个简单的数组,它工作得很好。 Now, I replaced it by a multi-dimensional array, and it crashes. 现在,我将其替换为多维数组,然后崩溃了。 Am I missing something obvious here ? 我在这里错过明显的东西吗?

You are getting the exception on this line 您在此行中遇到异常

icon[i][0] = b;

The size of this array is 0 so you can access the 0 element 该数组的大小为0,因此您可以访问0元素

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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