无法启动活动获取错误

时间:2013-03-03 10:41:07

标签: java android

活动无法启动我收到错误日志(红线):

03-03 12:29:50.492: E/AndroidRuntime(24207): FATAL EXCEPTION: main
03-03 12:29:50.492: E/AndroidRuntime(24207): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.makkuzu.hello/com.makkuzu.hello.Picture}: java.lang.ArrayIndexOutOfBoundsException
03-03 12:29:50.492: E/AndroidRuntime(24207):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
03-03 12:29:50.492: E/AndroidRuntime(24207):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
03-03 12:29:50.492: E/AndroidRuntime(24207):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-03 12:29:50.492: E/AndroidRuntime(24207):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
03-03 12:29:50.492: E/AndroidRuntime(24207):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 12:29:50.492: E/AndroidRuntime(24207):    at android.os.Looper.loop(Looper.java:130)
03-03 12:29:50.492: E/AndroidRuntime(24207):    at android.app.ActivityThread.main(ActivityThread.java:3693)
03-03 12:29:50.492: E/AndroidRuntime(24207):    at java.lang.reflect.Method.invokeNative(Native Method)
03-03 12:29:50.492: E/AndroidRuntime(24207):    at java.lang.reflect.Method.invoke(Method.java:507)
03-03 12:29:50.492: E/AndroidRuntime(24207):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
03-03 12:29:50.492: E/AndroidRuntime(24207):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
03-03 12:29:50.492: E/AndroidRuntime(24207):    at dalvik.system.NativeStart.main(Native Method)
03-03 12:29:50.492: E/AndroidRuntime(24207): Caused by: java.lang.ArrayIndexOutOfBoundsException
03-03 12:29:50.492: E/AndroidRuntime(24207):    at android.graphics.Bitmap.checkPixelsAccess(Bitmap.java:838)
03-03 12:29:50.492: E/AndroidRuntime(24207):    at android.graphics.Bitmap.getPixels(Bitmap.java:780)
03-03 12:29:50.492: E/AndroidRuntime(24207):    at com.makkuzu.hello.Picture.onCreate(Picture.java:188)
03-03 12:29:50.492: E/AndroidRuntime(24207):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-03 12:29:50.492: E/AndroidRuntime(24207):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
03-03 12:29:50.492: E/AndroidRuntime(24207):    ... 11 more

可能出现错误的代码是:

    colorbox = BitmapFactory.decodeResource(getResources(),
    R.drawable.color_picker);

    b = BitmapFactory.decodeResource(getResources(), R.drawable.picker);
    orgWidth2 = colorbox.getWidth();
    orgHeight2 = colorbox.getHeight();

    orgWidth = oldBitmap.getWidth();
    orgHeight = oldBitmap.getHeight();

    myColors = Bitmap.createBitmap(orgWidth2, orgHeight2,
            Bitmap.Config.ARGB_8888);
    newBitmap  = Bitmap.createBitmap(
            orgWidth, orgHeight, Bitmap.Config.ARGB_8888);
    pixels=pixels1=pixels2=pixels3=pixels4 = new int[orgWidth * orgHeight];
    ClrbxPx = new int[orgWidth * orgHeight];

    oldBitmap.getPixels(pixels, 0, orgWidth, 0, 0, orgWidth, orgHeight);
    newBitmap.setPixels(pixels, 0, orgWidth, 0, 0, orgWidth, orgHeight);

    colorbox.getPixels(ClrbxPx, 0, orgWidth2, 0, 0, orgWidth2, orgHeight2);
    myColors.setPixels(ClrbxPx, 0, orgWidth2, 0, 0, orgWidth2, orgHeight2);

    myFrame = BitmapFactory
            .decodeResource(getResources(), R.drawable.frame);
    canvas = new Canvas(newBitmap);
    canvas.drawBitmap(myFrame, 1, 1, null);

2 个答案:

答案 0 :(得分:2)

您应该像这样纠正ClrbxPx:

 ClrbxPx = new int[orgWidth2 * orgHeight2];

答案 1 :(得分:0)

logcat中的2个相关行是:

03-03 12:29:50.492: E/AndroidRuntime(24207): Caused by: java.lang.ArrayIndexOutOfBoundsException
03-03 12:29:50.492: E/AndroidRuntime(24207):    at com.makkuzu.hello.Picture.onCreate(Picture.java:188)

这基本上意味着你的一个数组不正确,你正在寻找索引之外。

我在代码中找到的唯一数组是:

 pixels=pixels1=pixels2=pixels3=pixels4 = new int[orgWidth * orgHeight];
    ClrbxPx = new int[orgWidth * orgHeight];

请检查Picture.Java文件的第188行并修复代码