I already make some search on the forum and on google about initializing and using a static field. But I can’t fix it alone so here is the simplified code:
...
import android.graphics.Canvas;
...
public class MyActivity extends Activity{
...
public static Canvas myCanvas = new Canvas();
public static float myMaxX;
public static float myMaxY;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
...
myMaxX = myCanvas.getWidth(); // <=== Error, the app crashes right there
myMaxY = myCanvas.getHeight();
...
...
}
...
}
Did I miss something ?
Yes!
You need to create a bitmap first:
But creating a static Canvas reeks of code smell. I don’t know what you’re trying to do with it, but I don’t think anything good will come of it. You may find you run in to loads of issues around concurrent read/write violations if you’re sharing this canvas between multiple threads.