【问题标题】:setting image drawable from a class从类中设置可绘制的图像
【发布时间】:2014-06-19 04:38:22
【问题描述】:

我正在尝试从非 Activity 类设置图像,可绘制名称保存在字符串中并且来自另一个方法,但问题是 getPackageName() 给了我这个错误:“无法解析方法 'getPackageName( )'" 我不知道该怎么做,这是我的代码:

String[] string = printArray(false, context);

image1.getResources().getIdentifier(string[0] , "drawable", getPackageName());
image2.getResources().getIdentifier(string[1] , "drawable", getPackageName());
image3.getResources().getIdentifier(string[2] , "drawable", getPackageName());

【问题讨论】:

  • drawable 在哪里?
  • @Rod_Algonquin 在 res/drawable-hdpi
  • 能不能把非活动的全班贴出来

标签: android image drawable


【解决方案1】:

试试image1.getContext().getPackageName()

【讨论】:

  • 这行得通,因为它没有给出错误,但是当我尝试这样做时它不起作用: image1.setImageResource( image1.getResources().getIdentifier(string[0] , "可绘制", image1.getContext().getPackageName()));
  • string[0] 里面是什么?
  • 不带c扩展名的图片名称
  • 你能检查一下 image1.getResources().getIdentifier... 的结果返回的不是 0 吗?
  • @user3272243 是什么解决了这个问题?
猜你喜欢
  • 2012-07-29
  • 2013-04-28
  • 1970-01-01
  • 2018-05-27
  • 1970-01-01
  • 2021-05-04
  • 2014-06-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多