Question

I'm trying to display 2 images on a canvas. I've loaded one, and it works fine. The second I've created, loaded and am displaying in the same manner, but the app force closes each time.

Both images are PNGs and are in res/drawable-mdpi.

Loading images:

private Bitmap stageBMP=BitmapFactory.decodeResource(getResources(), R.drawable.stagel);
private Bitmap magBMP = BitmapFactory.decodeResource(getResources(), R.drawable.mag);

Displaying images:

canvas.drawBitmap(stageL, 300,300, null);
canvas.drawBitmap(magBMP, 300,300, null);

If I comment out the final line, the app loads (displaying only the first image). Instead of displaying magBMP I show stageL twice, it works.

Working image:

Working image

Incompatible image:

Incompatible image

Stacktrace:

10-17 19:23:03.233: DEBUG/RobotronActivity(746): View added
10-17 19:23:03.403: DEBUG/MainThread(746): Starting game loop
10-17 19:23:03.543: WARN/dalvikvm(746): threadid=9: thread exiting with uncaught exception (group=0x40014760)
10-17 19:23:03.543: ERROR/AndroidRuntime(746): FATAL EXCEPTION: Thread-10
10-17 19:23:03.543: ERROR/AndroidRuntime(746): java.lang.NullPointerException
10-17 19:23:03.543: ERROR/AndroidRuntime(746):     at android.graphics.Canvas.throwIfRecycled(Canvas.java:999)
10-17 19:23:03.543: ERROR/AndroidRuntime(746):     at android.graphics.Canvas.drawBitmap(Canvas.java:1040)
10-17 19:23:03.543: ERROR/AndroidRuntime(746):     at com.cjs.robotron.Droid.draw(Droid.java:28)
10-17 19:23:03.543: ERROR/AndroidRuntime(746):     at com.cjs.robotron.MainGamePanel.onDraw(MainGamePanel.java:108)
10-17 19:23:03.543: ERROR/AndroidRuntime(746):     at com.cjs.robotron.MainThread.run(MainThread.java:52)
10-17 19:23:03.566: WARN/ActivityManager(73):   Force finishing activity com.cjs.robotron/.RobotronActivity
10-17 19:23:03.566: WARN/WindowManager(73): Failure taking screenshot for (216x135) to layer 21020
10-17 19:23:04.073: WARN/ActivityManager(73): Activity pause timeout for ActivityRecord{407afbf0 com.cjs.robotron/.RobotronActivity}
10-17 19:23:04.213: WARN/InputManagerService(73): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@406a7738 (uid=10036 pid=746)
10-17 19:23:04.223: WARN/InputManagerService(73): Client not active, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@40eefcf8
10-17 19:23:04.913: INFO/ActivityManager(73): Displayed com.cjs.robotron/.RobotronActivity: +3s510ms
10-17 19:23:10.893: DEBUG/dalvikvm(221): GC_EXPLICIT freed 18K, 5% free 6274K/6599K, paused 3ms+3ms
10-17 19:23:14.797: WARN/ActivityManager(73): Activity destroy timeout for ActivityRecord{407afbf0 com.cjs.robotron/.RobotronActivity}
10-17 19:23:15.973: DEBUG/dalvikvm(325): GC_EXPLICIT freed 8K, 6% free 6198K/6531K, paused 3ms+3ms
Was it helpful?

Solution

OK, I managed to find a solution.

Instead of loading images in one class and passing them to another to be displayed, I passed the context to the second class, and loaded them and displayed them there.

I confess I don't really understand why this should work, but for a scrappy solution it'll do for now.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top