GLWallpaperService icon indicating copy to clipboard operation
GLWallpaperService copied to clipboard

Error loading textures from resources - easy fix!

Open wernerlaurence opened this issue 11 years ago • 1 comments

Hello,

Firstly, thank you to everyone involved in this project - amazing work!

I had a problem getting the lesson 08 example working, kept getting an NullPointerException on loading the resource:

(...) public void loadGLTexture(GL10 gl, Context context) { //Get the texture from the Android resource directory InputStream is = context.getResources().openRawResource(l.werner.livewallpaper.hypnoclockgl.R.drawable.nehe_android); Bitmap bitmap = null; try { //BitmapFactory is an Android graphics utility for images bitmap = BitmapFactory.decodeStream(is);

        } finally {
                //Always clear and close
                try {
                        is.close();
                        is = null;
                } catch (IOException e) {
                }
        }

(...)

I found that the easiest way to fix this nullpointerexception is to modify the constructor in MyWallpaperService.MyEngine to include

renderer.setContext(getApplicationContext());

Full method: public MyEngine() { super(); // handle prefs, other initialization renderer = new MyRenderer(); setRenderer(renderer); setRenderMode(RENDERMODE_CONTINUOUSLY); renderer.setContext(getApplicationContext()); }

Anyways, hope this helps someone!

Thanks again to Mark F Guerra and Robert Green!

LW

wernerlaurence avatar Nov 21 '13 17:11 wernerlaurence

Thanks! I'll try this out next time I'm messing around with this project.

markfguerra avatar Nov 29 '13 00:11 markfguerra