Académique Documents
Professionnel Documents
Culture Documents
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: ×
Previous people have had the same error message, but the solution has always been to remove or modify some import "android.R". I have
no such import so I'm really lost
However, eclipse gives me this error when I paste over the code: "activity_main cannot be resolved or is not a field"
<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"
tools:context=".MainActivity" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
package com.example.mapssample;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
.title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("Kiel")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
1 de 5 02/12/2015 10:27
java - Android - activity_main cannot be resolved or is not a field - Stac... http://stackoverflow.com/questions/16995402/android-activity-main-can...
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
Thank you in advance for all your help. Sorry for some wonky indenting in my code, I kept giving four extra spaces to make it appear as
code and it got a little weird.
Thank you so much, do you have any idea what this means?
10 Answers
1) Where does your layout file exist? is it under layout folder then shouldn't it be
2 de 5 02/12/2015 10:27
java - Android - activity_main cannot be resolved or is not a field - Stac... http://stackoverflow.com/questions/16995402/android-activity-main-can...
R.layout.activity_main
com.example.mapssample.R;
3) R file is automatically generated if there's no error in your code. Sometimes when you get
can't resolve R file error means you have problem somewhere else in the project. Try clean
project. It might help.
4) You don't really need menu for testing this project. Also the activity_main file you showed is
already included in the project and that's in layout folder. Just comment the menu part and see.
I tried cleaning, let me look into the other two. – Ian Jun 8 '13 at 2:40
It's in the default location. The layout folder. You're right, it should be layout. – Ian Jun 8 '13 at 2:42
1 if following line is fine then your R is fine don't worry about that. setContentView(R.layout.activity_main);
Just comment following function onCreateOptionsMenu – Sharj Jun 8 '13 at 2:44
1 you have to figure out by looking at the error in your logcat what went wrong. – Sharj Jun 8 '13 at 2:47
1 many other people have faced this problem. try this stackoverflow.com/questions/15894143/… – Sharj Jun 8
'13 at 2:58
Your Code imported android.R so first remove this line import android.R;
so remove this line & press cntrl+shift+o (to import necessary packages)..
thanks its save my project dear @Anil Chahal – Adil Jun 20 at 1:16
I had same error im my app and i solved it by 1.removing import android.R; and 2. import
.3. After that clean the project.4. run project
com.example.yourpakagename.R;
I have three ways that help me to solve this problem, may be it works for you.
1. first clean than try to import your project like this: import
com.example.mapssample.R;
the activity_main.xml file maybe is wrong.I mean there is some weird symbol error inside the
file.you shold check it out.
3 de 5 02/12/2015 10:27
java - Android - activity_main cannot be resolved or is not a field - Stac... http://stackoverflow.com/questions/16995402/android-activity-main-can...
What weird symbol are you referring to? – Ian Jun 8 '13 at 2:41
I am not sure if this will solve your problem, but I have always declared fragment's type in xml
with the "name" attribute, not the "class" attribute. Try making your fragment
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment" />
Heh.. similar prob.. turns out somewhere along the line I had renamed the map fragment ID in
the XML layout file.. Was looking for a fragment ID that did not exists.. hope it helps someone
else.
I'm following the Deitel Android, How to program-book, and when I tried building my first own
application I experienced similar problems. What I did:
Working!
this is a common problem in eclipse. so 90% it is the problem with R.java file. so do following
steps it will work for shore
2.Ctrl+Shift+O.
com.example.mapssample.R.layout.activity_main
2 Please add some comments Why your solution should work. – Koopakiller Sep 27 at 10:30
4 de 5 02/12/2015 10:27
java - Android - activity_main cannot be resolved or is not a field - Stac... http://stackoverflow.com/questions/16995402/android-activity-main-can...
This does not provide an answer to the question. To critique or request clarification from an author, leave a
comment below their post - you can always comment on your own posts, and once you have sufficient
reputation you will be able to comment on any post. – James K Polk Sep 27 at 14:40
5 de 5 02/12/2015 10:27