Android library project: R.id cannot be resolved or is not a field

The library project looks great, but as soon as I import it into my main project, it shows me errors in every line that refers to the resource:

id cannot be resolved or is not a field

The main project does not show errors.

Due to the reason, I ask myself where android knows where to import resources, for example. in such lines:

RelativeLayout menuLayout = (RelativeLayout) this.findViewById(R.id.menu_layout); 

But this does not work:

 RelativeLayout menuLayout = (RelativeLayout) this.findViewById(net.bla.library.R.id.menu_layout); 

Any ideas?

EDIT: I found out that:

As soon as I turn on the library project, Eclipse duplicates gen / net.mylibrary.R from the library to the main application (therefore, there are now 2 packages in the gen folder: one from the application and one from the library copied). strange thing: R.id not found in copy. There are other differences, for example, the copy uses additional β€œfinal” definitions.

I really don't know why this could happen. Somebody?

+4
source share
4 answers

Eclipse sometimes likes to import android.R, and it causes errors similar to you.

Look at the import at the top of the file and delete it.

As said on " Using Eclipse | Android Open Source :

Note. Eclipse sometimes likes to add an import file to andor.R at the top of your files that use resources, especially if you ask eclipse to sort or otherwise manage the import. This will make your make break. Keep track of these erroneous import operations and delete them.

+5
source

Check if the resources are inside the res folder. All resources must be inside.

  • 0 Proyect
    • 0.1 src
    • 0.2 res
      • 0.2.1 layout
        • 0.2.1.1 main.xml
        • 0.2.1.2 menu_layout.xml
      • 0.2.2 drawings .....

Perhaps you are putting data in a project folder.

+2
source

I had a similar problem and managed to solve it. Here's what I did: When I call the main action of my library project, the onCreate function calls the setContentView method and uses R.layout.main as a parameter.

 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); Button btn = (Button) findViewById(R.id.main_button1); btn.setOnClickListener(this); 

findViewById returns null. It seems that the main.xml of the library project is being overestimated by the main main.xml project. So I just created a new xml layout main_libname.xml with the same contents as the main.xml library, and used this layout as a parameter for setContentView. It worked!

+2
source

I also had a β€œcan't find character variable ...” error in the R.id element defined in the library project. It is noteworthy that only R.id characters were not resolvable in the main project, although other R-fields (for example, R.layout) were clearly visible.

Fortunately, I was able to stumble upon a solution. It turned out that in my case I had a resource (layout file) defined in my main project, which had the same name as in the library project.

It was like a file system:

 top_level_project main_android_proj src main res layout activity_main.xml android_library_subproject src main res layout activity_main.xml 

In my subproject activity_main.xml there was such an identifier:

 <RelativeLayout....> <TextView android:id="@+id/special_text" ... /> </RelativeLayout> 

And this is how I try to reference id in my main Java project project:

 MainActivity.java: public void onCreate() { setContentView(R.layout.activity_main); TextView tv = (TextView) findViewById(R.id.special_text); tv.setText("Hello Android"); } 

To fix this problem, I renamed the offending file in my main project to something else.

It seems that you cannot duplicate layout names in your resources. Indeed, sdk docs indicate that you should be careful with resource name conflicts when using library projects:

  • Resource conflicts

Because the tools combine the resources of the project library with the projects of the dependent application, this Resource Identifier can be defined in both projects. In this case, the tools select the application resource or library with the highest priority and refuse another resource. When developing, keep in mind that shared resource identifiers are likely to be defined in more than one project and will be combined with the resource from the application or library with priority.

0
source

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