am very much a newbie when it comes to android development so bear with me. Im currently using Windows 8 / Eclipse.
My issue is, the findViewById function seems to have gone mad, although I could very well be using it wrongly.
The app was working fine, then I 'dropped' a few new TextViews on the page which seems to totally confuse it.
The findViewById function now either finds the wrong control completely, or doesnt find anything at all (and will return null). I've checked my activity_main.xml file and the id's are still correct.
Can anyone help?
This is a typical example of my usage:
public void toggleButtonNetwork_Click(View v) {
ToggleButton tb = (ToggleButton) this.findViewById(R.id.toggleButtonNetwork);//did work, now does not work!
}
The only insight I may add is that my R.java file looked like this when it was working:
...
public static final class id {
public static final int menu_settings=0x7f070004;
public static final int textViewGPS=0x7f070003;
public static final int textViewNetwork=0x7f070001;
public static final int toggleButtonGPS=0x7f070002;
public static final int toggleButtonNetwork=0x7f070000;
}
...
and now looks like this (broken):
public static final class id {
public static final int menu_settings=0x7f070006;
public static final int textView1=0x7f070004;
public static final int textView2=0x7f070005;
public static final int textViewGPS=0x7f070002;
public static final int textViewNetwork=0x7f070003;
public static final int toggleButtonGPS=0x7f070000;
public static final int toggleButtonNetwork=0x7f070001;
}
This is a quite common problem. Try and invoke the Project/Clean... on your project. It sometimes happens that the automatic generating of the R classes goes wrong and this will force them to be rebuilt.
Everthing is fine.
Always Just go to project -> clean
And than run .
Thats it.
This doesn't answer your question. I suspect that A--Cs comment is correct.
But, there is nothing to be confused about. findViewById is really simple.
When you compile your app, the compiler generates R.java and adds a line for each view in your layout XMLs (and strings, drawables, colours etc - anything that is a "resource") and gives it a unique ID.
public static final int toggleButtonNetwork=0x7f070001;
The ID will change as you change your resources, but it doesn't matter. When you use findViewById, you give it the "friendly name", in this case R.id.toggleButtonNetwork, which is compiled into 0x7f070001 since public static final int toggleButtonNetwork is a static constant.
When you inflate your view from XML, typically with setContentView, the view hierarchy of objects is built. The id of each object is the id found in R.java.
findViewById returns a reference to the object, of type View, which you then cast to whatever type of View it is which is why you use
= (Button)findViewById(R.id.toggleButtonNetwork);
Related
So I was happily coding away, and suddenly an error has appeared in my R.java class.
It's created a string, and says there is a "syntax error, and that I should delete the token 'sword'". I tried fixing it, but have instead created a duplicate string, "Long".
Here's the code:
public static final class string {
public static final int Long=0x7f050001;
public static final int Long Sword=0x7f050002;
public static final int app_name=0x7f050000;
public static final int desc=0x7f050003;
}
Does anyone know how to fix this error? Also, R.java doesn't like to be edited, so how to you save manual changes?
You named a field in your strings.xml called "Long Sword", change it to "Long_Sword", spaces aren't supported in the names.
Also, never ever touch the R.java file
public static final int Long Sword=0x7f050002;
Is not a valid field name.
Naming Conventions: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
You need to remove static from string class. A top level class cannot be static
public static final int Long Sword=0x7f050002; // not valid
Firstly, I have to say that I am just a beginner with the Android programming, and I may not understood things correctly. ^^
Secondly, my problem is that the R file (the one which create the different IDs of the objects) just stopped to create new ones.
I noticed that when I created a new image_button at the main_layout and when I tried to look for it on the MainActivity it wrote that it didn't exist.
Moreover, only after I modify the R file (which I should not touch according to the system warning), I have control over the new IDs.
And after breaking my head for two days - I decided to ask you,
The R file:
public final class R {
public static final class attr {
}
public static final class dimen {
/* Default screen margins, per the Android Design guidelines.
Customize dimensions originally defined in res/values/dimens.xml (such as
screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here.
*/
public static final int activity_horizontal_margin = 0x7F040000;
public static final int activity_vertical_margin = 0x7F040001;
}
public static final class drawable {
public static final int ic_launcher = 0x7F020000;
public static final int loading_i = 0x7F020001;
}
public static final class id {
public static final int action_settings = 0x7F080004;
public static final int button1 = 0x7F080001;
public static final int imbt = 0x7F080003;
public static final int textView1 = 0x7F080000;
public static final int tv1 = 0x7F080002;
}
public static final class layout {
public static final int activity_main = 0x7F030000;
public static final int firstpage = 0x7F030001;
public static final int loading_screen = 0x7F030002;
}
public static final class menu {
public static final int main = 0x7F070000;
}
public static final class string {
public static final int action_settings = 0x7F050001;
public static final int app_name = 0x7F050000;
public static final int hello_world = 0x7F050002;
}
The creation of the new object:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageButton
android:id="#+id/imgBtn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Yep, you shouldn't edit the R file directly. The problem you meet is normal. It should be causes by a file out of sync.
Hopefully, you could fix it by:
Option #1: Try to refresh whole project and 'Project -> build project'.
Option #2: If #1 doesn't work, hit 'Project - clean...', and then rebuild the project.
Option #3: Sometimes, #1 and #2 don't work. The last ultimate way,
which always works for me, is to delete the `gen` folder and rebuild the project.
Make sure that
Project --> Build Automatically
If it's already checkedm, run clean:
Project --> clean
If that doesn't work, then most likely there is an error in one of the files, most likely one of the layout XML files. Check the Problems view:
Window --> Show View --> Problems.
Fix all the problems (you can ignore the warnings).
Your last resort is to delete the R file (backup first) and then re-built (or clean).
It's one of the most common problems beginners face; even I faced this too. :)
The most obvious solution is to try cleaning and building your project. Sometimes it works, and most of the times it doesn't.
When cleaning and building doesn't work, then most likely you have some error in your code and that too in XML files. Android tools sometimes don't show which XML file has the error, so you manually need to open each file and check for errors. Once you have fixed the errors, your R.java will build automatically.
I hope this helps.. :)
Update:
This used to happen for Eclipse and ADT plugin. Android Studio is the savior. Start using it if you aren't already.
Clean your project and run it:
Project---> Clean then run
See Stack Overflow question Developing for Android in Eclipse: R.java not generating. It gives the perfect solution for you...
Change the build target version and clean the project. After completing that, change that build version to previous. It works for me.
This is the error, which is generating again and again.
I've cleaned my project.
public static final class id {
public static final int =0x7f07005b;
public static final int button1=0x7f070004;
public static final int button2=0x7f070005;
public static final int frameLayout1hardMatchup=0x7f070009;
There is no error in String.xml, here is my String.xml.
<string name="hello">Hello World, MemoryCardsActivity!</string>
<string name="app_name">Princess Memory Cards</string>
<string name="text_name">Congrates</string>
<string name="text_help">Turn over pairs of matching cards, tap to flip two cards, as the match happens, they get removed from the screen.Try to get all cards match in minimum moves and proceed to next level</string>
And also there is not any #+id kind of error.
What else could be the reason of this.
Any positive response would be really appreciated.. :(
You are missing a variable name
public static final int = 0x7f07005b;
instead
public static final int variableName = 0x7f07005b;
I produced a similar error by changing one of my layout XML files. I changed
android:id="#+id/image_frame"
to
android:id="#+id/ "
The error I see with this change is
public static final int =0x7f080010;
// Syntax error on token "int", VariableDeclaratorId expected after this token
This seems to be what #codeMagic was describing.
I'm trying to store a create database script (a rather lengthy one) in the strings.xml file (noobie here, haven't figured out a better place to put it yet) it does show up in the generated R class:
public static final class string {
public static final int app_name=0x7f040001;
public static final int create_database=0x7f040002; //this one here
public static final int hello=0x7f040000;
}
but when I try this in the code:
DATABASE_CREATE = R.string.create_database;
'create_database' is not available. Ti's simply not there, I get an error if I try to use it. Any ideas why this is so? Do those strings have length limitations? Can they only consist of a single line?
If that's the case, what's the right place to put my SQL create script?
Thanks for your answers.
R.string.create_database in the generated R is an integer (see your line with the comment). In order to get the string value, you need to call getString(R.string.create_database). See getString(int)
Strangely I'm getting really strange resource ids when R regenerates, that looks like:
public static final int #+id/#+id/music_thumb_img=0x7f060063;
public static final int #+id/music_main_text=0x7f060064;
When they should look like:
public static final int music_thumb_img=0x7f06001d;
public static final int music_main_text=0x7f06001e;
Needless to say, this R.java doesn't compile, but there doesn't seem like there's any problem in the java code nor the .xmls, at least not obvious ones that IDEs can pick up.
Has anyone encountered this?