Reputation: 14585
The library project looks fine, but as soon as I import it to my main project, it shows me errors on each line which is referencing a resource:
id cannot be resolved or is not a field
The main project shows no errors.
Of cause I ask myself where android knows where to import the resources from e.g. in lines like that:
RelativeLayout menuLayout = (RelativeLayout) this.findViewById(R.id.menu_layout);
But this works neither:
RelativeLayout menuLayout = (RelativeLayout) this.findViewById(net.bla.library.R.id.menu_layout);
Any Ideas?
EDIT: what I found out is:
As soon as I include the library project, Eclipse duplicates the gen/net.mylibrary.R from the library into the main app (so there are 2 packages in the gen folder now: the one from the app, and the copied one from the library). strange thing is: R.id is not found in the copy. There are some other differences too, like the copy uses an additional "final" for its definitions.
I really have no clue why this might happen. Someone?
Upvotes: 4
Views: 4746
Reputation: 5006
I was also having the "cannot find symbol variable ..." error on a R.id item defined in a library project. Notably, only R.id symbols were not resolvable in the main project even though the other R fields (eg. R.layout) were clearly visible.
Fortunately, I was able to stumble upon on a solution. It turned out that in my case, I had a resource (a layout file) defined in my main project which had exactly the same name as that in the library project.
This was what this looked like filesystem-wise:
top_level_project
main_android_proj
src
main
res
layout
activity_main.xml
android_library_subproject
src
main
res
layout
activity_main.xml
My sub-project's activity_main.xml had an id like so:
<RelativeLayout....>
<TextView android:id="@+id/special_text" ... />
</RelativeLayout>
And this is how I try to reference the id in my main project's java code:
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 issue, I renamed the offending file in my main project to something else.
It seems that you can't have duplicate layout names in your resources. Indeed, the sdk docs do point out that you should be careful with resource name conflicts when using library projects:
- Resource conflicts
Since the tools merge the resources of a library project with those of a dependent application project, a given resource ID might be defined in both projects. In this case, the tools select the resource from the application, or the library with highest priority, and discard the other resource. As you develop your applications, be aware that common resource IDs are likely to be defined in more than one project and will be merged, with the resource from the application or highest-priority library taking precedence.
Upvotes: 0
Reputation: 11930
Check out if the resources are inside the res folder. All the resources must be inside.
Maybe you are putting the data in project folder.
Upvotes: 2
Reputation: 301
I had a similar problem and I managed to resolve it. Here is what I did: When I call the main activity of my library project, the activity's onCreate method calls 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 returned null. It seems that main.xml of the library project is being overriden by the main project's main.xml. So I simply created a new xml layout main_libname.xml with the same content as library's main.xml and used this layout as a parameter for setContentView. It did work!
Upvotes: 2
Reputation: 5104
Eclipse sometimes likes to import android.R, and this causes errors similar with you are experiencing.
Look for the import at the top of the file, and remove it.
As it's stated on "Using Eclipse | Android Open Source":
Note: Eclipse sometimes likes to add an import android.R statement at the top of your files that use resources, especially when you ask eclipse to sort or otherwise manage imports. This will cause your make to break. Look out for these erroneous import statements and delete them.
Upvotes: 5