Resources$NotFoundException when trying to load animation - android

Our app on Google Play market has a weird exception being thrown on some devices. I see the following stack trace:
android.content.res.Resources$NotFoundException: File res/anim/ani_in_fade.xml from xml type anim resource ID #0x7f040000
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2185)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2140)
at android.content.res.Resources.getAnimation(Resources.java:894)
at android.view.animation.AnimationUtils.loadAnimation(AnimationUtils.java:71)
....
Basically, all the animations are in place in the res/anim folder. Most of the devices load and display them without a problem.
Any ideas what may be the reason for this? One of the ideas I have is that activity is being (or has been) destroyed at the point we try to load the resource, however the context is not null at that point of time...
Thanks,

Just in case anyone hits this problem... You need to update your proguard.cfg file and add the following lines:
-keepclassmembers class **.R$* {
public static <fields>;
}

Related

Android - Running my app has an error within super.onCreate

I'm trying to create a simple paint app using the Android Paint module.
When running my app, it gives me an error in "Run", saying that there's a problem at line 14, which is the line which by default is created in my mainActivity:
setContentView(R.layout.activity_main);
None of it is underlined in my code, I can't find any errors at all in my code (including in my activity_main.xml!).
MainActivity.java: https://pastebin.com/zhEbBYdQ
activity_main.xml:https://pastebin.com/jPsA0gMX
CanvasView.java: https://pastebin.com/GctVDz5B
Any ideas? I've tried clean + rebuild, I've tried com.example.R etc.
Full error log: https://pastebin.com/26TebLhy
Thanks in advance. (New to StackOverflow - I have problems putting in code directly to these posts so I hope PasteBin is okay).
CanvasView is in the package evansspeak.paint1, in main_activity however you are trying to inflate it from *com*.evanspeak.Paint1. Fix either one to be the same.
Check that the case matches as well (preferably all lower case)

Cannot resolve symbol 'gestures'

I have a OnGesturePerformed listener, a bunch of associated code, everything seems to work fine with the exception of this line:
private GestureLibrary gestureLib = GestureLibraries.fromRawResource(this, android.R.raw.gestures);
It tells me that the symbol gestures cannot be resolved. Tried various imports, does anyone know if there is a quick fix?
EDIT
Also tried:
com.package_name.R.raw.gestures
This time R cannot be resolved?
There's nothing in android.R.raw. You should import R.raw.yourRawFile from your own R file instead. Also make sure that you don't have any errors (if the file has a correct name).

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.

Android: Cant find .html file in res/raw after obfuscation

I have some html files in res/raw which I open in WebView. But after obfuscation they are unable to load.
I ran into exactly this same issue. I have my help html file in raw and after obfuscation I run my app and get an error that the file could not be found.
Here is my HelpActivity class:
public class HelpActivity extends BaseActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// requesting to turn the title OFF
//requestWindowFeature(Window.FEATURE_NO_TITLE);
// making it full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.help);
setTitle(getString(R.string.help_title));
WebView webView = (WebView) findViewById(R.id.webView);
webView.loadUrl("file:///android_" + getString(R.raw.how_to_play_zeewee));
}
}
I fixed this issue by adding the following to my proguard.cfg file:
-keepclassmembers class **.R$* {public static <fields>;}
-keep class **.R$*
You probably already have the first line, but this does not prevent the issue. Adding the second line eliminated the issue completly. I don't think the first line is still needed, but I have not tested that yet -- and since it currently works... ;).
Suggested in an answer in this question -
-keep class **.R$*
isn't the most elegant solution since it instructs ProGuard to preserve all of the R classes regardless of the package they are in.
Having the same problem with WebView, the error I see in my Logcat:
... E/AndroidProtocolHandler: Unable to open resource URL:file:///android_res/raw/$MISSING_RESOURCE_NAME.css
java.lang.ClassNotFoundException: Didn't find class
"my.app.package.R$raw" on path: DexPathList[[...
The instruction with maximum restrictions I added to my proguard-rules.pro file:
-keepnames class my.app.package.R$raw { public static <fields>; }
Obviously Since R class contains only fields and all of these fields have public static type, in practice, there should no be the difference between the one above and
-keepnames class my.app.package.R$raw { *; }
However, here I'm
NOT disabling shrinking and obfuscation for all of the rest inner classes in R other than raw.
targeting R in one specific package only.
That approach should be better in case if you have more than one module in your project that provides its own resources that may not be needed for the one particular APK you are building (having, let's say, more than one android_application modules – APK sources – in your project as well).
To understand the difference between -keepnames and -keep, please, refer to the following.
Distinguishing between the different ProGuard -keep directives - Tue May 29 04:10:50 MSK 2018
Create a file in raw folder
keep.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
tools:keep="#raw/*">
</resources>
That's it! It will keep all the files in raw folder, If you want to restrict it to only one particular file you can use
tools:keep="#raw/fileName"
Note that you should not add file extension in the end so it shouldn't be filename.html

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.

Categories

Resources