The method setImageDrawable(getResources().getDrawable(XX)) doesn't display the good image - android

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....

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());

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

Related

cucumber-calabash android - stop screenshots being overwritten in each html result

Problem: screenshots are being overwritten in each device's result html file
Scenario: I am running calabash-android to test a single mobile app running on multiple devices.
The SCREENSHOT_PATH environment variable is set as c:/AndroidApp/Results/deviceScreenshots/
This location will store all screenshots taken.
In a batch file an output format of html is specified and an output location of C:\AndroidApp\Results\device1\deviceId
For multiple devices we have a separate line per device, so device1, device2 etc.. etc..
When I finish the run and examine the screenshots for each device, I am seeing that screenshots are being overwritten and they are being taken from the environment variable location.
e.g: Environment variable folder has 10 screenshots
device 1 has taken 10 screen shots
device 2 has taken 10 screen shots
device 2 contains the same 10 screen shots as device one, due to the environment variable folder's imagenames being screenshot1.png, screenshot2.png etc etc
I have specified an unique device folder for each device html result output, so we do have unique result files, however the screenshots are being overwritten as being taken from the Environment variable folder.
any ideas? thanks all.
Graeme
Reading you question again I can see that you do set the path, but you use the same path for both runs? In case that is what you do and how you want to do it. My second option is probably most suitable for you(add a prefix to screenshots depending on device).
When you execute the test you can set screenshot path
SCREENSHOT_PATH=/tmp/foo/ calabash-android run
Link to Github about it https://github.com/calabash/calabash-android
So you could place screenshots in different folders.
Or you could add a prefix to the screenshots taken based on the device the test is run on. Like
screenshot({:prefix => "/tmp", :name=>"my.png"})
Link to Github https://github.com/calabash/calabash-android/blob/master/documentation/ruby_api.md
we tried a different approach by appending the screenshot name with a timeDate stamp instead of using an incremental integer counter.
added the following line of code to the operations.rb file. (not the failureHelpers.rb) to store the date in a specific format to the t1 variable, and output that t1 variable to the prefix.
##t1 = DateTime.now.strftime("%Y%m%dT%H%M%S%3N")
path = "#{prefix}#{name}_#{##t1}.png"
code snippet of operations.rb is now:
def screenshot(options={:prefix => nil, :name => nil})
prefix = options[:prefix] || ENV['SCREENSHOT_PATH'] || ""
name = options[:name]
if name.nil?
name = "screenshot"
else
if File.extname(name).downcase == ".png"
name = name.split(".png")[0]
end
end
##t1 = DateTime.now.strftime("%Y%m%dT%H%M%S%3N")
path = "#{prefix}#{name}_#{##t1}.png"
hope this helps, its not the solution to the original problem, just a re-thought out solution to the problem..phew...
anyway, thank you all for contributing , interesting puzzle indeed.
and thanks to my fellow tester here at work, was his work really not mine.

ClassCastException in Android After Changing Drawable Name?

I am writing an Android app and have run into a very strange situation. I have an animation-list xml file that I have created for an animated loading graphic named 'loading.xml' and I am setting it as the background for an ImageView in my code. It is something like this:
public void startLoading() {
if(!isLoading) {
loading.setBackgroundResource(R.drawable.loading);
loading.post(new Runnable() {
public void run() {
anim = (AnimationDrawable) loading.getBackground();
anim.start();
}
});
flMask.setBackgroundResource(R.drawable.mask_loading);
flMask.setVisibility(View.VISIBLE);
flMask.bringToFront();
isLoading = true;
}
}
This works wonderfully with no problems whatsoever. However, if I rename 'loading.xml' to 'anim_loading.xml' (to keep it inline with my usual naming scheme) I get a force close with
java.lang.ClassCastException: android.graphics.drawable.ColorDrawable
at the line where
anim = (AnimationDrawable)loading.getBackground();
After changing the name, it force closes, and if I change it back to just 'loading.xml' it works fine again.
I am really stumped on this one. I have tried creating a completely new file with the correct name, various clean/builds, closing/reopening Eclipse, closing/reopening the AVD, wiping the AVD and reinstalling fresh, I have tried renaming the xml file to something else entirely, and there are no other resources with the same names. So far, nothing has worked outside of leaving the file named 'loading.xml', which at least it works, but it is annoying. I haven't been able to find anyone with the same type of problem, which as vast as SO and the internet are, usually means that it's something stupid that I have missed somewhere along the line.
Does anyone have any ideas on why it will not let change the name on this file?
After giving up and just leaving it alone for a little while, I just stumbled upon the answer to this question!
Apparently, there is some sort of bug with Android that causes the first file in the res/drawable directory to be viewed as a ColorDrawable regardless of what it actually is. Before, when I was trying to change the name of the file, the new name put it at the top of the list in the directory and that was enabling this bug to show itself. Without the name change, the top item in my drawable directory just happened to be a ColorDrawable so I never noticed this weird bug until after I tried changing that name.
To solve the problem (or rather, to work around the problem) I created an empty dummy file in the res/drawable directory named a.xml. As odd as it may sound, it fixed everything!
You should save in drawable res/drawable/rocket.xml
It will fix the question.

getting Eclipse to see recently added Drawables (png artwork)

I'm having trouble getting Eclipse to see that I've just put a new image into my project when referencing that image using R.drawable.this_text
So I drag my png into myProject/res/drawable-hdpi in Eclipse's Project Explorer. The name of the image is this_text and it's a png!
So I go into my application and I want to put this on the screen; So here's the code for that...
private void setTitle()
{
ImageView title = new ImageView(this); //this extends activity
title.setBackgroundResource(R.drawable.this_text);
...
}
This I have done before and each time I do it I experience the same thing:
if i just dragged my this_text.png into the Project Explorer in eclipse, the word "this_text" will be underlined in red, and not allow me to compile or proceed. I try to F5 (refresh) the project. I try refreshing the image foldr, the src, the actual java...etc! I try closing and opening eclipse but nothing really works. So I quit for afew minutes and work elsewhere in my application, and eventually this error goes away!
Well I'm impatient today-- And I'd rather know how to solve this incase I need to do any quick programming! So does anyone know what to do?
I'm using eclipse Version: Indigo Service Release 1.
Thanks- Ethan
R files for android are generated. Rather than refreshing you should be able to CLEAN the project which should fix your issue. You could even just delete the error from the problems list which would allow for compilation to start and for the generated R file to be created. Or you could make a change to an xml which would also allow for the file to be regened. As always make sure you are up to date on your tool chain.

Android: access "system"-drawables

I'm currently working on an app to display the battery status and I'd like to use Android-drawables instead of own images to reduce the app size.
I've found this page which lists available images and the availability for each SDK-version:http://www.fixedd.com/projects/android_drawables_display
My question: How can I access the "system"-drawables? If you click on the link and choose the tab "Status", there are some battery-drawables like "stat_sys_battery_0", but I can't access it, Eclipse doesn't offer intellisense for it and won't compile the app if I use one of those drawables.
As those drawables are part of all SDK-versions, I'd think I should be able to use them, or are those "special" drawables protected in a way so they can only be used by system-functions (and not apps)?
Any idea is appreciated.
Select0r
Hope this is what you were looking for:
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
#Override
public void onReceive(Context arg0, Intent intent) {
int level = intent.getIntExtra("level", 0);
int batteryIconId = intent.getIntExtra("icon-small", 0);
Button toolBarBattery = (Button) findViewById(R.id.toolBarButton);
LevelListDrawable batteryLevel = (LevelListDrawable) getResources().getDrawable(batteryIconId);
batteryLevel.setLevel(level);
toolBarBattery.setBackgroundDrawable(batteryLevel);
}
};
I've found another link with information that not all drawables are public. It doesn't say why some drawables would be private, but I guess I'll have to live with the fact and copy the needed images to my app.http://androiddrawableexplorer.appspot.com/
NOTE: Some of the images in the Android jar are not public and therefore cannot be directly used (you can copy them to you own application, but can't reference them via the "android" package namespace).
There actually seems to be a way to access the system icons, but it's not really working as stated in the documentation, but I'll add it in case somebody is interested:
intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL, -1)
Will get you the resource-ID of the icon that matches the current battery-status:http://developer.android.com/reference/android/os/BatteryManager.html#EXTRA_ICON_SMALL
Extra for ACTION_BATTERY_CHANGED:
integer containing the resource ID of
a small status bar icon indicating the
current battery state.
However, it always returns the same icon, no matter what the actual battery level is. Finding the icon by just trying random numbers may work, but I don't know if the IDs are consistent throughout the SKD-levels as well as different machines, so I'd rather not rely in that.

Android: Any idea why cannot load drawable object?

There are several custom-made graphic objects (.png files) included in the project inside res/drawable map.
All elements are normally loaded and displayed in the user interface except two icons and so far haven't figured out what causes the problem.
The code which doesn't affect the user interface as it should is the following:
if (settings.isMute()) {
muteIcon.setIconImage(R.drawable.ic_volume_off_small);
} else {
muteIcon.setIconImage(R.drawable.ic_volume_small);
}
In both cases there is only ic_volume_small displayed on the screen and the Variables window in the IDE displays the following:
R.drawable.ic_volume_small = Class not
loaded : net.client.android.R$drawable
R.drawable.ic_volume_of_small = Class
not loaded :
net.client.android.R$drawable
The method (member of IconImage class) which should change the icon image is the following:
public void setIconImage(int imageFromResources) {
iconImage = BitmapFactory.decodeResource(getResources(), imageFromResources);
iconWidth = iconImage.getWidth();
iconHeight = iconImage.getHeight();
invalidate();
}
Does anyone know what could cause the described problem?
Thanks!
Assuming what you want is the drawable from the Android framework and not your own, you'd want to use:
android.R.drawable.ic_volumne_off_small
rather than
R.drawable.ic_volume_off_small
(notice the android prefix).
If you recently added file, please refresh (press F5) the drawable directory and R is going to be generated again. Once R is regenerated, you can use the newly added resource.

Categories

Resources