Android Java unable to find resource ID - java

On some devices there is no problem but on some such exception appears when opening src of ImageView.
The stack trace looks like this:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{pl.jawegiel.endlessblow/pl.jawegiel.endlessblow.activities.MainActivity}:
android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class
ImageView at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2666) at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727) at android.app.ActivityThread.-
wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478) at
android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at
android.app.ActivityThread.main(ActivityThread.java:6121) at java.lang.reflect.Method.invoke(Native
Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779) Caused by: android.view.InflateException:
Binary XML file line #11: Binary XML file line #11: Error inflating class ImageView Caused by:
android.view.InflateException: Binary XML file line #11: Error inflating class ImageView Caused by:
android.content.res.Resources$NotFoundException: Drawable (missing name) with resource ID #0x7f0800cd
Caused by: android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f0800cd at
android.content.res.ResourcesImpl.getResourceName(ResourcesImpl.java:228) at
android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:687) at
android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:571) at
android.content.res.Resources.loadDrawable(Resources.java:858) at
android.content.res.TypedArray.getDrawable(TypedArray.java:928) at android.widget.ImageView.
(ImageView.java:162) at android.widget.ImageView.(ImageView.java:150) at
androidx.appcompat.widget.AppCompatImageView.(AppCompatImageView.java:74) at
androidx.appcompat.widget.AppCompatImageView.(AppCompatImageView.java:69) at
androidx.appcompat.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:199) at
androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:119) at
androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1551) at
androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1602) at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769) at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727) at
android.view.LayoutInflater.rInflate(LayoutInflater.java:858) at
android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821) at
android.view.LayoutInflater.inflate(LayoutInflater.java:518) at
android.view.LayoutInflater.inflate(LayoutInflater.java:426) at
android.view.LayoutInflater.inflate(LayoutInflater.java:377) at
androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696) at
androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170) at
pl.jawegiel.endlessblow.activities.MainActivity.onCreate(MainActivity.java:50) at
android.app.Activity.performCreate(Activity.java:6682) at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2619) at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727) at android.app.ActivityThread.-
wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478) at
android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at
android.app.ActivityThread.main(ActivityThread.java:6121) at java.lang.reflect.Method.invoke(Native
Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
MainActivity.java:50 looks like this:
setContentView(R.layout.activity_main);
ImageView that causes problem looks like this:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/splash2"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:contentDescription="TODO" />
this splash2.png is located in res\drawable, res\drawable-hdpi, res\drawable-mdpi, res\drawable-xhdpi, res\drawable-xxhdpi and res\drawable-xxxhdpi so I guess in correct folders. So how to solve that?
Thank you in advance!

You may be able to solve this problem with a more generalistic approach:
Try using scalable vector graphics (SVG) instead of .PNG-images. VectorDrawable is more flexible if you need to match different screen resolutions.
PNGs can be transformed into scalable vector graphics with Inkscape or online PNG converter (https://convertio.co/de/png-svg/)
After that you can create a new VectorDrawable by loading the SVG. VectorDrawables can be assigned with app:srcCompat="#drawable/ic_VectorDrawableName"
Documentation:
A VectorDrawable is a vector graphic defined in an XML file as a set
of points, lines, and curves along with its associated color
information. The major advantage of using a vector drawable is image
scalability. It can be scaled without loss of display quality, which
means the same file is resized for different screen densities without
loss of image quality. This results in smaller APK files and less
developer maintenance. You can also use vector images for animation by
using multiple XML files instead of multiple images for each display
resolution.
VectorDrawable also requires much less RAM than .PNG.
You may find the cause of your errors in this thread:
Resources$NotFoundException drawable-xhdpi from drawable resource

Related

Vector images imported from SVG images not being recognised in ImageView

The SVG that I was attempting to import was not correctly formatted, this lead to the xml for the vecto image not being correctly formatted either Try the following samples here as these are all correctly formatted SVGs.
I am attempting to add a SVG image into a ImageView but i keep on getting an error every time I try to call its #drawable/ value. The file seems to be in the right location since when I use a PNg image it works, but it does not get recognised when its an SVG image.
The XML to add the image is :
<ImageView
android:id="#+id/statusImage"
android:layout_width="304dp"
android:layout_height="391dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/statusText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_temp" />
and the location of the image file is:
I have tried to modify the Gradle file so that it contains:
vectorDrawables.useSupportLibrary = true
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:design:28.0.0'
But that does not seem to solve the issue I'm having.
The error log seems to print that :
E/AndroidRuntime: FATAL EXCEPTION: main
Process: sc17dpc.individualproject, PID: 3467
android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class ImageView
Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class ImageView
Caused by: android.content.res.Resources$NotFoundException: Drawable sc17dpc.individualproject:drawable/ic_temp with resource ID #0x7f07006d
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ic_temp.xml from drawable resource ID #0x7f07006d
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:1166)
at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:917)
at android.content.res.Resources.getDrawableForDensity(Resources.java:1074)
at android.content.res.Resources.getDrawable(Resources.java:1013)
at android.content.Context.getDrawable(Context.java:630)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:463)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:203)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:191)
at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:102)
at android.support.v7.widget.AppCompatImageHelper.loadFromAttributes(AppCompatImageHelper.java:59)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:78)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:68)
at android.support.v7.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:182)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
at android.support.v7.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266)
at android.support.v7.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316)
at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:189)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:783)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:874)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:835)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at sc17dpc.individualproject.HomeFragment.onCreateView(HomeFragment.java:23)
and points to my class HomeFragment on the line:
View view = inflater.inflate(R.layout.fragment_home, container, false);
Any help is appreciated since the solutions that I've followed currently have not solved the problem.
*********EDIT*********
The SVG file is imported using the "New -> Vector Asset" built in feature in Android Studio and by using a local file.
The image file looks like:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="640dp"
android:height="640dp"
android:viewportWidth="640"
android:viewportHeight="640">
<path
followed by a load of information about the image path.
I have attempted to change app:srcCompat to android:src and this still gives the same error as before.
Can you try again?
android:src="#drawable/ic_temp"
You can't use viewportHeight and viewportWidth as 640
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="640dp"
android:height="640dp">
Source VectorDrawable
Issue has been sumarised at the top of the article. Thanks for the help.

Not able to intent to next layout

i'm working on my project and i created an interface of my app.
now i want that when ever i click in image view of Automation i go to my next layout. So i tried to write this code
the layout in which i want to move after clicking Automation is this :
But when i'm running app it's saying
Application error
Unfortunately,animation(name of my project) has stopped.
Logcat:
Caused by: java.lang.NullPointerException
at com.example.animation.dashboard.onCreate(dashboard.java:25)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2277)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365) 
at android.app.ActivityThread.access$600(ActivityThread.java:156) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:153) 
at android.app.ActivityThread.main(ActivityThread.java:5336) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
at dalvik.system.NativeStart.main(Native Method) 
What's mistake i'm making here ? i'm new to android so kindly help me out here.
Your view variable is not defined and you are trying to use it as it is null. But on the another hand, I dont think you need this, just go with findViewById without this view
It looks like view_rooms is not an activity - the second argument in your intent needs to be the context of the destination.
So if you want to go to new activity change your intent like this:
startActivity(new Intent(dashboard.this, SecondActivity.class));
You are using this in the dashboard activity line number 25
imgauto = view.findViewById(R.id.autoimg)
while your variable view is empty you have not assigned it something and also you are in activity so use it directly like this
imgauto = findViewById(R.id.autoimg)
try this and i am sure it will work perfectly.

From mipmap to drawable

I used to have the images of my app in the mipmap folders but I didn't have deferent resolutions ( I had the exactly same image in all 5 folders). In order to save space I moved those images in the drawable folder (except the launcher icons that are the only images that have different resolutions) . But when I open an activity that has some of those images I get the following crash error:
java.lang.OutOfMemoryError: Failed to allocate a
29304012 byte allocation with 16777216 free bytes and
23MB until OOM
at
dalvik.system.VMRuntime.newNonMovableArray(Native
Method)
at
android.graphics.BitmapFactory.nativeDecodeAsset(Native
Method)
at
android.graphics.BitmapFactory.decodeStream(BitmapFac
tory.java:624)
at
android.graphics.BitmapFactory.decodeResourceStream(Bi
tmapFactory.java:457)
at
android.graphics.drawable.Drawable.createFromResource
Stream(Drawable.java:1152)
at
android.content.res.ResourcesImpl.createFromResourceSt
ream(ResourcesImpl.java:1272)
at
android.content.res.ResourcesImpl.loadDrawableForCooki
e(ResourcesImpl.java:743)
at
android.content.res.ResourcesImpl.loadDrawable(Resourc
esImpl.java:585)
at
android.content.res.MiuiResourcesImpl.loadDrawable(Miui
ResourcesImpl.java:308)
at
android.content.res.Resources.loadDrawable(Resources.ja va:872)
at
android.content.res.TypedArray.getDrawable(TypedArray.ja
va:930)
at android.view.View.<init>(View.java:4182)
at android.widget.TextView.<init>(TextView.java:710)
at android.widget.Button.<init>(Button.java:109)
at android.widget.Button.<init>(Button.java:105)
at android.support.v7.widget.AppCompatButton.<init>.
(AppCompatButton.java:70)
at android.support.v7.widget.AppCompatButton.<init>.
(AppCompatButton.java:66)
at
android.support.v7.app.AppCompatViewInflater.createButt
on(AppCompatViewInflater.java:186)
at
android.support.v7.app.AppCompatViewInflater.createVie
w(AppCompatViewInflater.java:109)
at
android.support.v7.app.AppCompatDelegateImplV9.create
View(AppCompatDelegateImplV9.java:1035)
at
android.support.v7.app.AppCompatDelegateImplV9.onCre
ateView(AppCompatDelegateImplV9.java:1092)
at
android.view.LayoutInflater.createViewFromTag(LayoutInfl
ater.java:794)
at
android.view.LayoutInflater.createViewFromTag(LayoutInfl
ater.java:752)
at
android.view.LayoutInflater.rInflate(LayoutInflater.java:883)
at
android.view.LayoutInflater.rInflateChildren(LayoutInflater.j
ava:846)
at
android.view.LayoutInflater.rInflate(LayoutInflater.java:886)
at
android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:846)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:886)
at
android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:846)
at
android.view.LayoutInflater.inflate(LayoutInflater.java:522)
at
android.view.LayoutInflater.inflate(LayoutInflater.java:430)
at
android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at
android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppC
ompatDelegateImplV9.java:287)
at
android.support.v7.app.AppCompatActivity.setContentView(
AppCompatActivity.java:139)
at
com.soon.coming.endturnbutton.themes.onCreate(themes.java:38)
at
android.app.Activity.performCreate(Activity.java:6852)
at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2700)
at
android.app.ActivityThread.handleLaunchActivity(ActivityT hread.java:2808)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at
android.app.ActivityThread$H.handleMessage(ActivityThre ad.java:1541)
at
android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:165)
at
android.app.ActivityThread.main(ActivityThread.java:6365)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.r
un(ZygoteInit.java:883)
at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
I have faced this issue before, this was because i had a very very large image inside my drawable folder to be loaded (out of memory).
What i did, is to drag all my drawables to this site to generate Android assets and returned them back to my drawables with the new sizes (and resolutions as well like hdpi,....etc).
This means you are out of memory. What you can do to save space is move the images back into the mipmap folder but you only need it in one of them. You do not need to put it in all five. It is not the best practice, but it would work.
use this link to create appropriate resolution images(hdp,xdp, xxdp) and then put these images in related drawable resource directory
https://romannurik.github.io/AndroidAssetStudio/

Error inflating class ImageView in android

whenever i hit the imageview i get this error, this happens to me when i switch to test my app on different screen sizes, here is my xml
<ImageView
android:id="#+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="28dp"
android:layout_marginRight="28dp"
android:layout_marginTop="20dp"
app:srcCompat="#drawable/search"/>
and here is the error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ahmed.electionadmin, PID: 30789
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ahmed.electionadmin/com.example.ahmed.electionadmin.Search}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class ImageView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
I have tried so many solutions that asks me to replace src with srccompat and so on, but still is working, ant help would be appreciated
To use srcCompat you need to add vectorDrawables.useSupportLibrary = true to your build.gradle file:
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
Or you can use src instead of srcCompat to resolve the error.
android:src="#drawable/search"
Replace this:
app:srcCompat="#drawable/search"
With:
android:src="#drawable/search"
Pay attention, not only src with srcCompat, also app with android.
Let me know if it works.
Explanation: You're not using a support library ImageView, but a regular one. app namespace is usually used for support library, same as srcCompat. So you needed to change those 2 things, not just one. That's why merely changing the srcCompat with src yielded no results.
i fixed it by uploading my image inside draawable no 24
you are doing like fitting image to different resolutions , you should have image with different resolutions like hdpi xhdpi , you you can use vector drawable image

Custom Layout in Homescreen Widget

I am developing a homescreen widget that has a number of different sized buttons. To maximize the use of space, I want to use a flow layout for this widget (which for some crazy reason, android has no native implementation of).
To solve this, I have tried using third party implementations and implemented the layout myself, however, I always get an exception when loading the widget:
W/AppWidgetHostView: updateAppWidget couldn't find any view, using error view
android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class [redacted].FlowLayout
Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class [redacted].FlowLayout
Caused by: java.lang.ClassNotFoundException: Didn't find class "[redacted].FlowLayout" on path: DexPathList[[zip file "/system/priv-app/Velvet/Velvet.apk"],nativeLibraryDirectories=[/system/priv-app/Velvet/lib/arm64, /system/priv-app/Velvet/Velvet.apk!/lib/arm64-v8a, /system/lib64, /vendor/lib64, /system/lib64, /vendor/lib64]]
My layout for the widget looks only like this:
<[redacted].FlowLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#09C"
android:padding="#dimen/widget_margin"
android:id="#+id/widgetRoot"
android:orientation="horizontal">
</[redacted].FlowLayout>
Assuming that the redacted namespace is correct, is there any way to do what I want or is it impossible to use a custom layout with a homescreen widget?
Home screen widgets are wery limitted things, they doesn't support custom layouts: https://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout

Categories