I have a problem when attaching my Bitmap to an ImageView. This is the error I get:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: accelerometer.example.com.accgame, PID: 25897
...
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
I know it is a null pointer exception but I have no idea why getResources() returns null... Here is my constructor class:
public SimulationView(Context context) {
super(context);
mXOrigin = 50;
mYOrigin = 50;
mHorizontalBound = 100;
mVerticalBound = 100;
//Sensor Manager
sensorManager = (SensorManager)context.getSystemService(SENSOR_SERVICE);
Bitmap ball = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
iBall.setImageBitmap(ball);
mBitmap = Bitmap.createScaledBitmap(ball, BALL_SIZE, BALL_SIZE, true);
Bitmap basket = BitmapFactory.decodeResource(getResources(), R.drawable.basket);
mBasket = Bitmap.createScaledBitmap(basket, BASKET_SIZE, BASKET_SIZE, true);
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inDither = true;
opts.inPreferredConfig = Bitmap.Config.RGB_565;
mField = BitmapFactory.decodeResource(getResources(), R.drawable.field, opts);
WindowManager mWindowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
mDisplay = mWindowManager.getDefaultDisplay();
}
Basically the error highlights the line iBall.setImageBitmap(ball);
In terms of the image itself, I made sure to place it in the drawables folder. It is a .jpg file and I have shrunk it by a lot as well.
Initialize iBall in OnCreate() method
iBall = (ImageView)findViewById(R.id.image);