Android imageView id not showing - android

I have an imageView in my main.xml file, that isn't setup with a source initially. I want to be able to dynamically change the image resource within the code. so lets so i have my imageView setup like this:
<ImageView
android:id="#+id/countryImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/CountryImageContent"/>
So i try to access within my code like this:
ImageView img = (ImageView) findViewById(R.id.countryImage);
The problem is here. Eclipse is giving me an error saying that "countryImage cannot be resolved or is not a field". A quick peek into my R.java file is showing me that the imageView isn't actually in there. Why is this happening? Thanks.

look into your drawable and layout if there is any error
if not try cleaning your project
Go to Project > clean > select your project
if not then look into your imports for
your.packagename.R remove it

Project -> Clean.
That is the only solution to this.
Also make sure that among your import statements, you do not have this : import android.R
Check this link for a detailed procedure.
Note : I am assuming that there are no errors in your layout and drawable files, as errors in them may also lead to this problem.

make sure all resource files have only lowercase letters , numbers and the underscore character ("_") .
only if there are no errors in creating the R file (in the "gen" folder) , you can use the id of it.

I think it is the problem to giving the name of #string
Please Remove all UpperCase Character from #string/CountryImageContent and replace it with small character.
R.java file doesn't support any Uppercase character. It gives the same error when you declare the file name of any resources Like :- Image, XML, #string, etc...
Just rename CountryImageContent with country_image_content. it will work perfect.

Another option to try in this case is: File --> Invalidate Caches / Restart...

Related

Android Studio resource dialogue is not showing my project drawables

I have encountered the following issue in Android Studio: when I select the "..." to specify the source drawable file for an ImageView, in the Project tab I don't see the project drawables, even if they do appear into the drawable folder on the left pane, but instead a long list of (I guess) system drawables (whose name is starting with "abc_") and no trace of the custom files I have successfully pasted into the project drawable folders.
If I try to write manually a reference to the correct resource in XML (android:src="#drawable/main_title"), I get the following error popup:
Rendering Problems Couldn't resolve resource #drawable/main_title. Failed to convert #drawable/main_title into a drawable.
Does anybody have any idea of what's happened? Thank you in advance.
You're trying to put in android:src a string, not a Drawable. If you want to apply drawable to your ImageView src you should use somthing like this:
android:src="#drawable/your_image_drawable"
I just found my own mistake: I pasted the drawable files inside the drawable folder, while they must reside inside the res folder. Now it works.
Thanks for your replies.

Android XML : Could not resolve resources

So I'm building a game in Android, and I have just created a huge XML file. When I switch to graphical layout in ADT, I'm getting the right layout, so no problems there.
But below the graphical layout, There are two messages:
Couldn't resolve resource #drawable/ic_launcher
Couldn't resolve resource #string/app_name
I have no idea what is causing these problems, as both words ic_launcher and app_name are not found anywhere in my XML.
But I found ic_launcher in all *hdpi folder under res/drawable/ and the string app_name was in res/values/string.xml
Should I have entered XML tags somewhere inside my code? If so, can you please tell me under which tag it should be?
[My XML is composed entirely of nested LinearLayouts and Buttons]
Thanks!
Make sure you don't have errors in other resources (layouts, values, drawables, etc.).
And then clean/refresh/build the project

Importing R.layout.main - Android

I am working on an Android application that will be using images and so I am working being able to grab an image and display it. Though when I put in setContentView(R.layout.main); It tells me that main does not exist but offer to put into the layout file or R.java. When trying to place it into the file and save the R.java files tells me it reverted it right back to the original purpose, why is that?
From your comments you said you have activity_main.xml and not main.xml
So change to
setContentView(R.layout.activity_main);
ALso if you have imported R.java remove the same. Make sure there are no errors in your resource files. Clean and build.
If you have a imageview in activity_main.xml with id gimg1 then
ImageView iv = (ImageView) findViewById(R.id.gimg1);
and import
import android.widget.ImageView;
Its the same to initialize if you have others view's in your xml
R.java file is generated automatically, you can't edit it. In your case tyr to clean your project via Project->Clean

can't edit specific xml file in Eclipse

There are similar questions, but none address my specific issue.
I'm rather new to Android development and Eclipse but was able to follow the tutorials A OK.
In fact, I'm able to edit activity_main.xml from a tutorial, but now, in a test project I'm working on, I can't edit my new activity_main.xml (again, different project, same filename).
I created the file by right-clicking on the layout folder then selecting NEW > OTHER > Android XML Layout File. I was able to copy some of the other file so that my file now looks essentially like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
</LinearLayout>
When I type, the cursor moves around (looping through the text) but no characters appear on the screen. Again, in the other XML file I can edit with no problem.
I'm hoping that this is just some newbie problem.
I have had similar problems, though not this exact problem. Sometimes simply restarting Eclipse, or whatever editor you use, can help. Occasionally, Eclipse will not detect changes in your xml so you need to clean and rebuild the project. You can do this by
Winodow --> Project --> Clean...
and choose your project
I know there was an issue that I had which was similar with the editor jumping/replacing lines and such which I believe was fixed in a newer version of Eclipse. If I can find a source later I will edit
Clear all of text in layout activity_main.xml (in code view)
Drag and drop new LinearLayout in layout (in graphical view)
Go to Project -> Clean

is there one way to turn on code assist in animation xml

Eclipse give me a good experience in code assistence.But when it comes to animation xml files and i enter alt+/,it pop up nothing.So it make me very easy to type a wrong attribute name because of my poor english.can you give me a hand?
thanks.
Make sure that your animation xml file is location in anim folder not animation folder
I am not sure what you mean by "animation xml file" (the CEGUI ones? Android ones?), but, as explained in "eclipse editor, ex: xml editor, code assistance. how it works?", any xml file with auto-completion needs a DTD or Schema informtion.
Sometime, the DOCTYPE at the beginning of the xml file can prevent the auto-completion to work.

Categories

Resources