Question

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?

Was it helpful?

Solution

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!

OTHER TIPS

You should save in drawable res/drawable/rocket.xml

It will fix the question.

screenshot

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