I have a main, auto-generated class. I'm want to draw the simple ic_launcher png to my image view that I declared in xml.
My main class:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyCanvas can = new MyCanvas(this);
//ImageView img = (ImageView) findViewById(R.id.imageView1);
//img.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
}
}
The class I made:
public class MyCanvas extends View{
public MyCanvas(Context context) {
super(context);
ImageView img = (ImageView) findViewById(R.id.imageView1);
img.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher));
}
}
You will notice I commented out setting the bitmap in my main activity. That is because it was a test. The code works perfectly in the main activity but fails in the other class. I don't know how logCat works, but I see a "null pointer exception." I'm almost positive the error is when I load the ImageView.
I tried this: context.findViewById(R.id.imageView1); to no avail.
Note, the image's id is imageView1.