markfguerra / GLWallpaperService

Please submit issues and pull requests to the main repository
https://github.com/GLWallpaperService/GLWallpaperService
Apache License 2.0
218 stars 114 forks source link

Error loading textures from resources - easy fix! #31

Open wernerlaurence opened 10 years ago

wernerlaurence commented 10 years ago

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

markfguerra commented 10 years ago

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