简体   繁体   中英

Eclipse for android: Issues with R

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>

Try removing the line:

import android.R;

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

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?

R.java它是一个自生成的文件,您需要重新构建它才能生成它,忽略警告并“构建清理”您的文件

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM