I have an Activity that has a main.xml with an ImageView. I then launch an other activity and wand to show the same picture that is in the first activity. My second activity's layout is created programmatically with linearlayout (ll). I then use the following:

    ImageView img = (ImageView) findViewById(R.drawable.logo);
    ll.addView(img);

    setContentView(ll);

but i get img to be null, It doesnt work if i change it to R.id.logo which is defined at main.xml

有帮助吗?

解决方案

First create a new imageview by doing ImageView img = new ImageView(this); Then set your drawable to any image you have in your "drawable" folders for example: img.setImageResource(R.drawable.my_image);

Then you can add it to your linear layout like you do above and then setContentView.

What you were doing above to findViewById does not make sense because you did not set content view. So just follow the setups i outlined above.

其他提示

if you are calling findViewByID from main.xml you first need to setCOntentView as main.xml

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top