I was trying to load a drawable programmatically in my kotlin app via
resources.getDrawable(R.drawable.XXX)
and all I got was:
Method threw 'android.content.res.Resources$NotFoundException' exception.
whatever the target drawable was.
After trying different things, I finally tried on a newly created Kotlin project, only to find out it didn't work either.
I then created a new Java project, and everything worked flawlessly in this one.
I found nothing about people having the same problem online, I can get the "Resources" object in both project, but it just can't find drawables in the Kotlin one.
Is there anything to do to make it work that I don't know about?
Edit:
I'm getting the resources like this in the newly created project:
override fun onResume() {
super.onResume()
val drawable = resources.getDrawable(R.drawable.ic_launcher_foreground)
}
This happens whatever the target resource is, drawables, mipmaps, colors...
I'm on Android Studio 3.5.3 with gradle 3.5.3 and Kotlin 1.3.61, API level 26
Project resources are the one added on project creation
I found a temporary fix, and mostly something very interesting.
Taking the example of "ic_launcher_background" as a random drawable, Java gives this result :
While the exact equivalent code in Kotlin gives us this :
I don't understand why, but when using a project generated the Kotlin way, the Ids retrievable via R just don't match the ones used by the Resources class.
So for now I'll use the "getIdentifier()" method to fix my problem, but it really isn"t something you'd want to do naturally.
If someone that has a better understanding of what is going on here could unfold this mystery, that would be greatly appreciated.
Try this:
override fun onResume() {
super.onResume()
val drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_launcher_foreground, null)
}
Related
So to not repeat my initial question that might give some background I'll just attach a link to it
So I currently need to iterate through a ChipGroup, and when I try to introduce "for each" or "until", android studio gives me an unresolved reference notice.
I've checked and double checked and I don't think I am writing it in a bad way, but the studio doesn't let me use it for some reason.
The rest of my code which is entirely kotlin is completely fine.
questionButton.setOnClickListener {
questionChipGroup.forEach
for ( i in questionChipGroup until questionChipGroup.childCount){
}
postQuestion(
questionTitle.text.toString(),
questionDetails.text.toString(),
questionTags.text.toString(),
System.currentTimeMillis().toString()
)
}
In the examples above, both until and forEach are red and are basically unrecognized by Android.
I tried to invalidate the cache and restart and that didn't help.
Am I missing something?
You are using ChipGroup API in android. It is basically a sub-type of ViewGroup, so to iterate through a ChipGroup.
for ( index in 0 until questionChipGroup.childCount){
val childView = questionChipGroup.getChildAt(index)
// Process childView here
}
forEach in Kotlin only works on Iterator. Because ChipGroup does not implement Iterator interface, that why you cannot use forEach on it.
until in Kotlin is not an infix function, but also an extension function. It only works on Byte, Short, Int, Long type, that why you cannot call it from questionChipGroup (its type is ChipGroup).
I know this is an old question. Still I am updating my answer so it may help somebody.
Check if your project supports Kotlin. Also, your Android Studio Kotlin plugin and the Kotlin runtime/compiler that you use in the project should match.
Update the IDE Kotlin plugin version to match the project by doing this: Tools -> Kotlin -> Configure Kotlin Plugin Updates -> Check for updates now
Check this to know various ways to use until
I'm trying to bind an android SDK for voice chat (zoom sdk).
They have two .aar files (zoomcoomonlib.aar and zoomsdk.aar)
I know I have to create separate binding project for each .aar and then reference them.
While binding zoomsdk.aar I'm getting the below error
The type `Com.Zipow.Videobox.Onedrive.ErrorEventArgs' already contains a definition for `P0' (CS0102) (B14)
In the .aar file I navigated to the package com.zipow.videobox.onedrive; to the interface IODFoldLoaderListener
And below are the contents of it
So it seems parameter String var1 of method onError is causing the issue.
And xamarin studio generated obj/debug/api.xml confirms it (below screenshot) that onError will have first parameter named p0:
So in this scenario I change the metadata.xml to give this parameter a meaningful name.
Like below screenshot:
But even after doing that I am getting same error. That error didn't resolve.
Moreover now if I see the obj/debug/api/.xml file I see the contents for the class IODFoldLoaderListener remains the same.
So changing the metadata.xml has no effect it seems.
Your definition needs to be changed quite a bit. Here is an example that solves the same problem:
<attr path="/api/package[#name='com.emarsys.mobileengage.inbox']/interface[#name='ResetBadgeCountResultListener']/method[#name='onError' and count(parameter)=1 and parameter[1][#type='java.lang.Exception']]" name="argsType">ResetBadgeCountResultListenerOnErrorArgs</attr>
Please note the /interface and argsType items here as your initial definition is incorrect. You would then change the parameters to strings instead of java.lang.Exception from my example.
I have created an Android library project that i am linking with my Android app. In the drawable folder of the library project, I have added a few images. Now I am trying to access these images from a non-activity class in the same project but they are not being accessed when I try to access them using the following snippet
Drawable drawable=context.getResources().getDrawable(R.drawable.image);
The import of R file is correct and the image id is being generated correctly in the R file. The context is correct. I tried refreshing and cleaning/building the project multiple times but nothing seems to work.
Help!
[Note: Answer copied from here.]
Yes you can if you know the package name of your library.
Resources.getIdentifier(...)
You can do:
getResources().getIdentifier("res_name", "res_type", "com.library.package");
ex:
R.id.settings would be:
getResources().getIdentifier("settings", "id", "com.library.package");
I'm not sure I understand the quest completely (the problem you are having is that the method does not return a drawable?
I'm not sure you about how you are getting the context, but you could try this:
Drawable drawable = ContextCompat(getContext(), R.drawable.image);
If you are storing the context in a variable, you can pass the variable instead of using getContext().
Additionally, the method you are using to get the drawable is deprected, so you shouldn't be using it anymore.
Let me know if it helped you and remember to upvote/select this as correct answer if it did, cheers.
Probably a stupid question here but I just started programming in Android so bear with me.
https://gist.github.com/gabrielemariotti/ca2d0a9f79b902b19a65
I'm following this project to implement a GridViewPager on my Android Wear application. It works fine, only the background images I'm trying to use are not displaying. It only displays a black screen.
The issue is this getBackground() method, the imageReference in particular.
public ImageReference getBackground(int row, int col) {
SimplePage page = ((SimpleRow)mPages.get(row)).getPages(col);
return ImageReference.forDrawable(page.mBackgroundId);
}
The tutorial shows that an Image Reference should be returned however I am getting build errors saying that ImageReference cannot be resolved. So would I have to create a class or interface for ImageReference? Or do you set an attribute on the images themselves?
Can anyone shed any light on this?
Cheers in advance.
As of version 1.1 of the Wearable Support Library (which FragmentGridPagerAdapter is part of), the methods have changed quite a bit: they no longer use any sort of ImageReference class - the new method, getBackgroundForPage, simply returns a Drawable.
I'm attempting to create an Activity and unfortunately every time I want to grab one of my XML components it gives me a RunTimeException (NullPointer).
Anytime I use code such as:
TextView tv = (TextView) findViewById(R.id.myView); //I get the exception
The same happens for any components I attempt to find with that method. I can't quite figure out why. I know it isn't due to the Activity not being in the Manifest because it's the only Activity in the test app I made. (The one set up by default).
Oddly I can still use setContentView(R.id.myView). It just doesn't seem to want to find anything when using the findViewById method.
Info that might be of use:
I am currently using NetBeans as my IDE.
I have done multiple 'clean and builds' as was suggested in another question. Android -findViewById question
Has anyone run into this issue before? If so, what was the solution?
If need be, I can provide sample code of when this is happening.
Don't pass in a view ID to setContentView, pass in a layout resource ID:
setContentView(R.layout.layout_name);
If you still have problems, post your layout file.
It is very sure that you R.java is not properly generated.
Delete R.Java in netbeans IDE and Re-build the project.
Hope it resolves your query.