Method1 (Not working)
I declared an Imageview in XML
<ImageView
android:id="@+id/imageView1"
android:layout_height="wrap_content"
android:layout_weight="0.21"
android:layout_width="100dp"
android:layout_gravity="center">
</ImageView>
Then I declare it in code
ImageView iv=(ImageView)findViewById(R.id.imageView1);
Then I download a image from web (image)
iv.setImageBitmap(image);
setContentView(iv);
Method2 (working)
Instead of XML i just declare
ImageView iv=new ImageView(this);
Then I download a image from web (image)
iv.setImageBitmap(image);
setContentView(iv);
The question why Method1 doesn’t work and Method2 works?
Method 1 should give a force close error as you are using findViewById before setContentView.
FindViewById tries to find the view inside the layout you specified in setContentView. So setContentView should be called first.