14

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

I'm trying to get a sample android google maps program running.

I'm following this tutorial. http://www.vogella.com/articles/AndroidGoogleMaps/article.html

However, eclipse gives me this error when I paste over the code: "activity_main cannot be resolved or is not a field"

This happens in MainActivity in this snippet of code

  public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;}

The specific error is in the "R.menu.activity_main" part.

Here is my activity_main.xml file.

<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> 

And here is my full MainActivity.java class

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;

public class MainActivity extends Activity {
  static final LatLng HAMBURG = new LatLng(53.558, 9.927);
  static final LatLng KIEL = new LatLng(53.551, 9.993);
  private GoogleMap map;

  @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)));

// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

    // Zoom in, animating the camera.
    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
  }

  @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.

New, error log.

Thank you so much, do you have any idea what this means?

06-07 22:45:25.226: E/AndroidRuntime(29901): FATAL EXCEPTION: main
06-07 22:45:25.226: E/AndroidRuntime(29901): java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
06-07 22:45:25.226: E/AndroidRuntime(29901):    at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.app.Activity.onCreateView(Activity.java:4713)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:260)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.app.Activity.setContentView(Activity.java:1893)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at com.example.mapssample.MainActivity.onCreate(MainActivity.java:23)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.app.Activity.performCreate(Activity.java:5058)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2174)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.app.ActivityThread.access$700(ActivityThread.java:141)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1267)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.os.Looper.loop(Looper.java:137)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.app.ActivityThread.main(ActivityThread.java:5059)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at java.lang.reflect.Method.invokeNative(Native Method)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at java.lang.reflect.Method.invoke(Method.java:511)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at dalvik.system.NativeStart.main(Native Method)
Sharjeel
  • 15,588
  • 14
  • 58
  • 89
Ian
  • 561
  • 4
  • 8
  • 15

11 Answers11

23

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)..

Anil Chahal
  • 2,544
  • 2
  • 22
  • 19
17

1) Where does your layout file exist? is it under layout folder then shouldn't it be

R.layout.activity_main

2) You still have to import your projects R file

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.

Sharjeel
  • 15,588
  • 14
  • 58
  • 89
  • It's in the default location. The layout folder. You're right, it should be layout. – Ian Jun 08 '13 at 02:42
  • What do you mean by important my project's R file? – Ian Jun 08 '13 at 02:43
  • 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 – Sharjeel Jun 08 '13 at 02:44
  • Alright, i tried it both ways. I both changed it to layout, and tried commending it out. In both situations, I had no errors, then ran it and got "Unfortunately, MapsSample has stopped" – Ian Jun 08 '13 at 02:45
  • 1
    you have to figure out by looking at the error in your logcat what went wrong. – Sharjeel Jun 08 '13 at 02:47
  • Wow, didn't even know that was something I could do. Check back in a minute to the main post to see the error log. – Ian Jun 08 '13 at 02:51
  • Thanks so much for your help so far, so close to getting this to work. Do you have any idea what the error log means? – Ian Jun 08 '13 at 02:54
  • 1
    many other people have faced this problem. try this http://stackoverflow.com/questions/15894143/java-lang-noclassdeffounderror-com-google-android-gms-rstyleable-in-android – Sharjeel Jun 08 '13 at 02:58
7

I had same error im my app and i solved it by 1.removing import android.R; and 2.import com.example.yourpakagename.R;.3. After that clean the project.4. run project

andiDeve
  • 77
  • 1
  • 4
1

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;
  2. right click on project go to properties--> android --> select target.
  3. clean
Mohit
  • 1,045
  • 4
  • 18
  • 45
1

when you faced a red wave line under the Capital "R" just hold mouse there and Select second Line "import your_package_name.R" Because it link all related files of your project

Don't select first fix that says "import android.R"

and it still shows error under activity_main, It means can't find those files in your project,check that file names like activity_main.xml etc..

Hope it Works..

  • One More thing.. Don't use "CTRL+SHIFT+O" or similar shortcut keys..because it selects the first fix and it imports "import android.R" which is may useless for your project.. – Prakash Parmar Dec 12 '17 at 05:18
0

the activity_main.xml file maybe is wrong.I mean there is some weird symbol error inside the file.you shold check it out.

anonym
  • 3
  • 1
0

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" />
brainmurphy1
  • 1,082
  • 1
  • 19
  • 24
0

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.

0

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:

In windows environment variables: HOME=%USERPROFILE% JAVA_HOME=\Program Files\Java\jre7\bin (or what/where you have it)

In the code: Mainactivity.java: Added Import com.deitel.welcome.R (that is package.R) Changed the call R.layout.activity_main to R.layout.main (check the generated R.java file)

Working!

0

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

1.Remove your import android.R

2.Ctrl+Shift+O.

3.clean the project

varunRaj
  • 31
  • 2
-1

Try the following

com.example.mapssample.R.layout.activity_main
Koopakiller
  • 2,838
  • 3
  • 32
  • 47
raj
  • 1