1

I am making a brick-breaker game for android and attempting to change the image of the background. On the first run of the game this works 100% of the time.

     mBackgroundImage = BitmapFactory.decodeResource
                (gameView.getContext().getResources(), 
                R.drawable.planet3);

planet3 is in the res/drawable folder.

If i return to the previous screen and start a new game, I get a NPE roughly 90% of the time. if I use a resource that was provided with the framework i am working on which is in the same folder as "planet3", Strangely it works 100% of the time regardless of whether its a new game or the first game etc.

Why could this be happening some of the time and not others. I generally find that when it does work it is because i am debugging it and stepping through line by line, however this could just be coincidence.

The code i have at the moment is as follows, TheGame is the current Thread the game is running on, the code i pasted above is in the constructor of this, I.E. everytime a new game is made, the background should be set. you can also see the file structure on the left to verify that the file "background" is in the same folder as the "planet3": enter image description here

as far as the error is concerned i don't have an actual error message as it just says that MOOC has stopped working, and then restarts the program.

JabbaWook
  • 677
  • 1
  • 8
  • 25

2 Answers2

0

May be this happen if you are using a heavy image or Please post all your code here and exception as well for better resolution

0

I think that the reason for NPE in your case is :

The BitmapFactory is unable to find the context parameter gameView.getContext()

You should use :

mBackgroundImage = BitmapFactory.decodeResource
                (getApplicationContext().getResources(), 
                R.drawable.planet3);

If you have saved context in member variable i.e. mContext through constructor then you can use that :

mBackgroundImage = BitmapFactory.decodeResource
                (mContext.getResources(), 
                R.drawable.planet3);
Kushal
  • 8,100
  • 9
  • 63
  • 82
  • should getApplicationContext be a user made function? – JabbaWook Mar 17 '15 at 05:17
  • No.. Its Android API.. provided in **Context** class – Kushal Mar 17 '15 at 05:21
  • i cant seem to use it? i have the context class imported, do i need to be using it in a certain class? – JabbaWook Mar 17 '15 at 05:30
  • just use `getApplicationContext().getResources()`. Is it giving error ? – Kushal Mar 17 '15 at 05:35
  • If it gives error then declare an Object of Context class and initialise it in your constructor and use it like `mContext.getResources()` – Kushal Mar 17 '15 at 05:36
  • ye i just get a method undefined error and creating a Context called mContext and setting it to gameView.getContext() in the constructor and using that instead still gives me the same problem. – JabbaWook Mar 17 '15 at 05:40
  • Please follow http://stackoverflow.com/a/16679202/1994950 and pass Context like this in your constructor – Kushal Mar 17 '15 at 05:42