Issue
I have a simple problem, which has been answered many times on SO before, but I don't understand the answers and I can't get my code to work.
I want to create a bitmap from a drawable.
public class Helicopter extends Sprite {
private Context context;
private Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.heli1_east_rot);
public Helicopter(Context c) {
context = c;
}
}
public class TitleScreen extends State {
private Helicopter heli;
public TitleScreen(Context c) {
heli = new Helicopter(c);
}
}
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Game game = new Game(this, null);
game.pushState(new TitleScreen(game.getContext()));
setContentView(game);
}
}
I have also tried super.getGame().getContext()
in the TitleScreen class, but both attempts gives the same error in LogCat:
01-26 13:28:32.217: E/AndroidRuntime(1537): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloandroid/com.example.helloandroid.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
Why does it have to be so hard to create a bitmap, when I can access the drawable like this private Image wallVerImage = new Image(R.drawable.heli);
without any reference to the context?
How do I fix my code?
Thanks
Solution
the context in
private Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.heli1_east_rot);
does not specified, so you cannot refer to resources. Put initialization here:
public Helicopter(Context c) {
context = c;
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.heli1_east_rot);
}
Answered By - anil
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.