1

I am trying to do a location app and am in the trouble shooting phase right now. I am down to my last two problems and they both relate to R. blah blah. It says main is either not a field or cannot be resolved and the same message but with mapview instead of main. Below are my imports and the lines of code where the issue is.

Code:

    setContentView(R.layout.main);
    mapview = (MapView)findViewById(R.id.mapview);

Imports:

    import android.R;
    import android.R.layout;
    import android.R.id;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.widget.Toast;

    import com.google.android.maps.GeoPoint;
    import com.google.android.maps.MapActivity;
    import com.google.android.maps.MapController;
    import com.google.android.maps.MapView;

I've tried everything. I know people keep saying to take out android.R but that is where R.layout and R.id are found in the jar files. I have right clicked the folder and validated the code. I have gone to Project/Clean and done that. No luck. Please help? And this is my first post so I'm sorry if its formatted wrong. I thank you in advance for any help you give me. Here is my main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Waiting for location..."
        android:id="@+id/lblLocationInfo"
    />
    <com.google.android.maps.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
        android:apiKey="0vFrUOhHMkbahT9zXqiz_DuNVWfPqlEyqcO8ftg"
    />
    </LinearLayout>
Tucker Watts
  • 121
  • 2
  • 10

4 Answers4

2

Try removing the line:

import android.R;

And then do Build Clean, this can fix the issue.

Lengoman
  • 904
  • 2
  • 12
  • 22
2

unless you are using resources from android you shouldn't need the lines

import android.R; 
import android.R.Id;
import android.R.layout;

Remove those lines

R is resolving to androids R and not com.yourpackage.yourproject.R.

Remove the import if it's not needed. If it is your need to be explicit in your code. ex.

ImageView.setDrawable(android.R.ic_menu_search); //uses an android resource

findViewById(R.id.mapView); //uses your resource file

setContentView(R.layout.activity_main); // must have an activity_main.xml file in YourProject/res/layout

Edit:

changes to your code below:

//import android.R;
//import android.R.layout;    //comment or delete these lines
//import android.R.id;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

setContentView(R.layout.activity_main);  //changed main to activity_main
mapview = (MapView)findViewById(R.id.mapview);

Edit 2

At the very top of your MainActivity.java it should have a line that starts with package what does the rest of that line say?

In your AndroidManifest.xml there is also an entry for package what does it say?

bytebender
  • 7,371
  • 2
  • 31
  • 54
  • I'm new to programming...that made almost no sense. Can you explain what that means? – Tucker Watts Jul 31 '12 at 19:07
  • No problem I updated the answer... Android has it own resources. If you start by removing the 3 lines I suggested and do a clean and build and see where that gets you. – bytebender Jul 31 '12 at 19:09
  • ah ok that makes sense. I removed those lines but I still have those two errors. – Tucker Watts Jul 31 '12 at 19:12
  • In your project under res/layout is there a main.xml file? – bytebender Jul 31 '12 at 19:13
  • Yes there is. activity_main.xml – Tucker Watts Jul 31 '12 at 19:15
  • Then your code should read `setContentView(R.layout.activity_main);` That should resolve one of the issues. – bytebender Jul 31 '12 at 19:17
  • So should I just copy/paste those 3 lines in that you gave me? – Tucker Watts Jul 31 '12 at 19:21
  • Tucker, it is clear you don't know what you're doing. I suggest you read more about R. It's the Resources file that is auto generated. It assigns integer values to pictures, sound files, etc so you can call it in your application. What you're doing is you are importing android.R. You're supposed to use your own R file located in `gen/R.java`. So remove the 3 `android.R` imports. – tolgap Jul 31 '12 at 19:23
  • I know I don't know what I'm doing. That's why I'm asking for help. I got this far I just need help with 2 issues. I realize that's what I'm doing. I put those three in to see if being more specific about where I was importing them from would fix it. It obviously didn't. So I took them out. – Tucker Watts Jul 31 '12 at 19:26
  • It now says that R cannot be resolved to be a variable. That did fix the other problem with mapview and main though. – Tucker Watts Jul 31 '12 at 19:30
  • Did you try clicking Project->Clean another though do you have any other errors in your layout or other files located in res directory? – bytebender Jul 31 '12 at 19:31
  • I greatly appreciate your help. You answered the question that I had posted with which has been plaguing me for about a week. Thank you very much! – Tucker Watts Jul 31 '12 at 19:39
  • They both say the same thing. I noticed and fixed it earlier. They both say "com.example.googlemaps" – Tucker Watts Jul 31 '12 at 19:44
  • I found my problem!! To anyone else having this issue, go to the following site! http://www.mybringback.com/travis-android-help/1/hello-world/ – Tucker Watts Jul 31 '12 at 19:57
0

R.java it's a self generated file, you need to re-build it for it to get generated, ignore the warnings and "Build Clean" your file

Braiam
  • 1
  • 11
  • 47
  • 78
Isaac Gonzalez
  • 1,734
  • 1
  • 16
  • 22
0

This might also have to do with your project setup. What I typically do when I create a new project is setup 'Order and Export' options. That way you do not have to import any R.* packages.

In Eclipse, right click on your project go to Properties -> Java Build Path -> Order and Export. Just make sure that yourprojectname/gen folder is above the yourprojectname/src folder.

shri046
  • 1,148
  • 11
  • 12
  • Ok! I will definitely try that. What exactly will that do? – Tucker Watts Jul 31 '12 at 19:08
  • As others have already mentioned the R.java file is auto-generated when you do a project build. The Order and Export basically sets the project dependencies. You can read more about it here. http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.jdt.doc.user/reference/ref-properties-build-path.htm http://stackoverflow.com/questions/2737486/what-is-the-use-of-order-and-export-tab-in-java-build-path – shri046 Jul 31 '12 at 19:31