开发者

setImageResource throws an exception to load image on the ImageView

I am trying to display the image on the screen using ImageView. setImageResource fails. Here is my XML :

<FrameLayout
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:layout_weight="1">
 <ImageView 
     id="@+id/batteryLevelview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center"/>
</FrameLayout>

  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 
     ImageView batteryLevelImage = (ImageView) findViewById(R.id.batteryLevelview);
     batteryLevelImage.setImageResource(R.drawable.notconnected);
   }    

What's wrong in my code? Can you please help me to find what's wrong. I would like to update image on the ImageView object based on criteri开发者_StackOverflow社区a. I tried using LinearLayout and that did not help.

thanks.


String uri = "drawable/notconnected";

// int imageResource = R.drawable.notconnected;
int imageResource = getResources().getIdentifier(uri, null, getPackageName());

Drawable image = getResources().getDrawable(imageResource);

ImageView batteryLevelImage = (ImageView) findViewById(R.id.batteryLevelview);
batterLevelImage.setImageDrawable(image);

              OR
batterlLevelImage.setImageResource(getResources().getIdentifier(drawable/notconnected, null, getPackageName()));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜