Question

i succeed in using this code in a new project, but, when copying it in my actual code, the image displayed is not the good one everything is well made (image is in the folder drawable, every data well declared, ...) image begin with "phoXX", where XX is an integer (between 0 and 62) i have an imageview (named imageView1) and a TextView (to debug, named textView1) in which i display the ID (which one is good when checking in R.java...)

here is my code :

private Context mContext;
ImageView  imageC=(ImageView)findViewById(R.id.imageView1);
TextView  Tex=(TextView)findViewById(R.id.textView1);       
Drawable drawableX = this.getResources().getDrawable(android.R.drawable.ic_dialog_alert);
Random random_monster = new Random();
int lerand = random_monster.nextInt(62);
int id = mContext.getResources().getIdentifier("drawable/pho"+lerand, "drawable",mContext.getPackageName());
if (id>0)
{
    drawableX = getResources().getDrawable(id);
}
imageC.setImageDrawable(drawableX);
Tex.setText(Integer.toString(lerand)+":"+id);

So, as i say, an image is displayed, a number is displayed, but pho29 for exemple show the image named pho48 (and pho48 doesn't display pho29 if you ask) ID is 2130837535, which is what appears in R.java : public static final int pho29=0x7f02001f; pho48 is :
public static final int pho48=0x7f020034;

for some number, the image displayed is the good one, for other, the image is not the good one (and it's always the same image displayed for the same number....i mean for pho29, it's ALWAYS pho48 which is displayed, in every one of my activity)

I really don't understand what i miss...Thx all for any kind of help

Edit : below some matching (on the left side the number which should be displayed, on the right the number displayed)

2->6
17->32
19->34
24->41
43->43
51->51
36->55
37->56
38->57
56->56
59->59
61->61

As you can see, 56 is displayed by 56 AND 37....

Was it helpful?

Solution

If the same code and set of image resources work fine in one project and not in another, you should consider whether the compiled resources in the broken project are actually correct. I have found when using the automatic build feature in Eclipse that the resources don't get recompiled when I expect them to. For instance if the last thing I did before deploying an app was to change the resources. Saving a code file change seems to drive the build system harder.

You should try doing a clean then a rebuild. In Eclipse, on the Project menu choose "Clean..." and pick your project (or let it clean them all). Eclipse will then do a full rebuild. Or on the command line run "ant clean" followed by "ant debug".

Also you are using this line:

int id = mContext.getResources().getIdentifier("drawable/pho"+lerand, "drawable",mContext.getPackageName());

Here is the API spec:

public int getIdentifier (String name, String defType, String defPackage)

You don't need to specify the type in the name field, because you are specifying it in the defType field. I don't think it will solve your problem, but this should work at least as well:

int id = mContext.getResources().getIdentifier("pho"+lerand, "drawable",mContext.getPackageName());

OTHER TIPS

Try to put the imageview without source in its definition, sometimes I have the problem when an imageview has source set in xml, changing its drawable in runtime not work, the first image is always visible

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top