Eclipse error with android: id cannot be resolved or is not a field

I just started playing with Android development, and already with an attempt to make a button, I ran into a problem. The error indicated in the following code is directly on "R.id.button1". He says the identifier cannot be resolved or is not a field. Do I need to manually reference every single object that I create in the layout XML file? I found that it really worked, but it seems like for each button I want to do a little ...

package com.example.helloandroid; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class HelloAndroid extends Activity { /** Called when the activity is first created. */ private Button button1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button1 = (Button)findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener() { public void onClick(View v) { finish(); } }); } } 
+4
source share
9 answers

I spent a lot of time (two weeks) because of the same problem, until I found that the problem was not mine, but Eclipse. I think there are many people with the same problem.

Just try it: Save the project, close Eclipse and reopen it. So simple.

+9
source

Do I need to manually reference each individual object that I create in the XML layout file

Yes, otherwise you cannot do anything with these species. This is actually not so bad. Thus, every time you create a view in your XML and want to reference it, enter the ID:

 <View android:id="@+id/the_id"/> 

And then from your code, you can reference it using the R class. You can enter R.id.the_id in the example and then Ctrl + Shift + O to force Eclipse to automatically import the necessary files.

You can speed up your productivity using frameworks like Roboguice ; I think this is for lazy people.

+3
source

This answer does not apply to this question (looking at the code you provided). Just add it if someone else stumbles here and the above answers will not help.

If cleaning up (Project β†’ clean) does not help or saving and restarting eclipse does not help either, check for the following incorrect import.

 import android.R; 

Which Eclipse sometimes adds by mistake auto-import (Ctrl + Shift + O). Delete this line (import) and do the following: D

+2
source

After this EXCELLENT tutorial, I ran into the same problem. After reading Carmello's answer ( September 17, 2011 07:23 ) I just clicked File-> Save All, and voila, "button0" was automatically detected and even the syntax highlighted.

+1
source

If "R.id.button1" is not defined, you will get a compilation error, as you saw. If you do not define it in the layout, it will not be determined.

You do not need to specify every object that you create in the layout, but you do if you try to reference it with "R. *". You can manually create buttons and other objects that are not specified in the layout.

0
source

I looked through the same problems for a while. Plz remember to define the following:

 <View android:id="@+id/button1" /> 

if you use an identifier in your .java class.

 Button b =(Button) findViewById(R.id.button1); 

Saying that the identifier defined in the XML file must match the identifier in findViewById ().

0
source

Go to the "R.java" file in the "gen" folder and check if your "button1" is present under the "id" class. If not, this may be the reason you got this error. When you use the "R.id." instruction, make sure that it is present in the corresponding class, in this case in the "id" class.

0
source

R.id is a generated object that assigns int numbers to resources. Try this go to your gen / mypackage / R.java and delete the file. As you can see, it is recreated. This file provides static links, where the context is more relevant to the dynamic state of your application. If you have syntax errors that prevent automatic regeneration of this R.java file, you will get many errors or R. Like everyone else, you can click the "Save" button on the icon or ctl + shift + s on the windows. You can clear the project / clear the project and clear 95% of these exceptions. Yes, eclipse is so buggy, but netbeans does not support Android. this link may help

Good luck.

0
source

Do it, any of them will help you

  • Project β†’ Clean, Right-click β†’ Fix project properties.
  • Restart Eclipse
  • make some fake manifest changes and save
  • check the console for error messages
  • check your selectable folder, check if image names match the rules
0
source

Source: https://habr.com/ru/post/1333304/


All Articles