I am trying to define a layout of my custom view with the id cannonView as the following.
<com.myapp.CannonView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cannonView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent" />
This XML is placed under the layout folder. The problem is that the R.id.cannonView is not generating.
I found the answer myself. The problem was that I did not specify the right qualified class name at first. That was the reason ADT did not create this constant in first place.
When I corrected the problem with the right qualified class name, R.id was still not there. I even tried deleting R.java so that ADT regenerated it but that didn't help either.
I had some other compilation errors as well. When I fixed those, R.java generated itself and the id in question was there.
Related
Surely I used #id after declared #+id and my App runs well with my code, what confused me is that when I preview my xml files, the reference #id seems not work at all and the layout was in my library project not main,which was dependenced by aar.
If I change the layout_below="#id/tv_1 to layout_below="#+id/tv_1, the preview will work well.
Or I declare it in id.xml files id my library proj,and use #id/tv_1, preview works well too.
all solutions above seems make no difference after I run my main project,they all works well. I just can't understand why preview has problems,and how the '+' works when preview a xml file.
Examples:
<RelativeLayout ..>
<TextView android:id="#+id/tv_1" ../>
<EditText android:id="#+id/et_1"
android:layout_below="#id/tv_1" ../>
</RelativeLayout>
#+id creates a new entry to R.java class which stores and identify assets.
#id simply refers to the previous created assest.
I am new in andorid development.
I have created my android first project according to http://developer.android.com/training/basics/firstapp/creating-project.html
And here is the code that I have as the project was created(no changes are done in code by me)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
Here is the screenshot of the Graphical layout
http://imageshack.us/a/img35/5284/android1s.png
So, it looks fine for now, but if I want to change the text Hello world to , say, hello, so the code's part looks like android:text="#string/hello", I get the error
NOTE: This project contains resource errors, so aapt did not succeed, which can cause rendering failures. Fix resource problems first.
and
Couldn't resolve resource #string/hello, and the text in graphical layout becomes
#string/hello, as it is in this image
http://imageshack.us/a/img826/7735/android2p.png
But I noticed one thing:
If after this error I go to res/values/strings.xml and add String with name and value hello, like
http://img842.imageshack.us/img842/8108/android3s.png
The error disappears and I can see the word hello in the graphical layout.
I tried these so far
Using ctrl+shift+s like it is described here https://stackoverflow.com/a/14462434/932473
Closing and reopening Eclipse.
cleaning, rebulding
removing the entire project and creating again.
The values folder does exist in res.
None of these questions solved my problem
Eclipse could not resolve string resource error
Can't Resolve Android Resource Strings
UPDATED
If I write like this android:text="hello"
I get this warning in eclipse
Hardcoded string "hello", should use #string resource,
I do not get it every time I want to use some string like android:text="#string/hello2", I have to first manually specify hello2 it in strings.xml ?
In res/values/strings.xml
<string name="hello">Hello</string>
Then refer the string in your textview as
android:text="#string/hello"
I guess you do not have something similar to above. Hence you get resource not found exception coz the resource is not defined in strings.xml
or
You can do as below
<string name="hello_world">hello</string> // change the string in strings.xml
android:text="#string/hello_world"
For the question in the comment
You can just use
android:text="hello"
the #string bit means its a resource called hello in your xml.
So you can also add the resource to your xml if you wish. Its known to be better practice, for internationalization.
I keep getting a FileNotFound Exception, and quite frankly, it makes no sense. Here are my files:
custom.xml
<item android:drawable="#drawable/easymode"></item>
main.xml
<Button android:id="#+id/guess"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:layout_marginBottom="20dp"
android:onClick="myClickHandler"
android:background="#drawable/custom"></Button>
I've tried moving the background tag above the others to see if that was causing it to fail, it didn't work. I've moved the custom.xml file into a drawable folder, and into the ldpi and mdpi folders but it keeps giving me the same error. Why can't it find my custom.xml when it's quite clearly there? R.java also lists it, so I know it's generated the link to it successfully... Help!
Have you tried building the R file again after saving? It can be a little confusing when working with the Android SDK in Eclipse (i'm guessing you are using Eclipse). After adding your new layout xml, are the ids for buttons and strings correctly parsed and identified in your code?
I would do a save all and then compile the project and then run it and see if this sorts your problem out.
The order of the tags makes no difference here as the layout xmls are compiled into a compact resource.
If it still cannot find the xml I suggest deleting the xml, restart Eclipse and create it again under layout, add your code above to the xml, save, compile and run and see if that sorts it out.
i am customizing an action bar, and i am using inflater and styles.
i added a menu.xml to a menu folder in project which i inflated into the onOptionsMenu,
the menu.xml contains the action bar item's.
Everything worked fine, until i renamed menu.xml to actionBar_menu.xmlusing refractor.
Now i can't even add it to the Inflater. it's not even declared in R.java
How do i solve the problem any hint or solution?
Thank you
Edit:
Thank you guys for your answers.
i did what you asked so i changed my action bar to actionb_menu.xml, but it still not working.
otherwise it's now declared in R.java, but eclipse editor doesn't recognize it
(ctrl+space), and even if i ignore this and continue typing the name of the file in the inflater it it still underlined as an error.
any solution for that??
Your original problem was: You can only use lowercase letters, numbers and underscores in your layout XML file name. As stated by #midoalageb
This is the answer to your edit.
Because your id wasn't initially in your R.java when you tried to do
R.id.....
eclipse has imported android.R; into your class file.
You need to go up to the top of your file delete this import:
import android.R;
then attempt to do:
R.id.whatever
again. When you press ctrl+space after R. ensure you select com.your.package.R and not android.R
hey hosni habeby look do like this,
open your new xml file the one you re named it and go to
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.something. app.OldJava" />
and replace the context value to your new java file the one you renamed also.. like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.something. app.MyNewActivityJava" />
in eclipse when you changed java file's name and it xml.. eclipse will not touch the values in the xml so we have to change it our selves also eclipse will add an import line may not refer to your R file.. if so remove it and type one that refers to your R file of your project not of Android or any library
You can only use lowercase letters, numbers and underscores in your layout XML file name. So rename your file to actionbar_menu.xml
Right click on Project. Refresh it first--> then clean it --> then rebuild it -->if there are necessary code changes then do it--> it should work.
also rename it in all small case chars
Try cleaning the project. If it still says R cannot be resolved then there should be something wrong with the xml file. Check your xml file for some extra characters that you mistyped or something of that kind. Your R file will not be built if even one xml file has an error!
I am creating a few layouts with the root element being merge. inside the merge element i have a ScrollView containing a TextView. Here is the file:
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:id="#+id/scroll"
android:padding="6dp"
android:layout_below="#id/headingLayout"
android:layout_above="#id/tabsLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/aboutTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
</merge>
I am getting 2 errors: there is no resource to match the layout_below and layout_above names i have put in.
I have another layout xml file including very similar components, some of which also reference #id/headingLayout and #id/tabsLayout. Both of these XML layout files do not contain the component with name headingLayout or tabsLayout
Why is it that one layout file has no errors about these references and the other layout file does? What the crap am i doing wrong, the app will build and run like i expect, until i add this merge to another layout?
I have even tried copying the xml from the working layout file, to find that it gives the same errors, something must be wrong with my new layout file. I have tried cleaning, rebuilding, opening/closing eclipse..
Wow, this is a strange fix i have found for this. the layout i have given in my question above(the one giving errors) is named about.xml. the layout with no errors which also includes merge and references to other xml components is named home.xml. the xml file including the referenced components is named base_layout.xml.
in the eclipse Package Explorer under the layout folder, items are listed alphabetically. so the home.xml file came after base_layout.xml, and could therefore reference the components inside it. Since about.xml came before base_layout.xml, about.xml could not reference anything inside base_layout.xml.
so i just renamed base_layout.xml to aaa_base_layout.xml so it would be first, and all errors go away.