简体   繁体   中英

Java. layout cannot be resolved or is not a field

Hi I am new to Android programming and working on Eclipse. I am having an error "layout cannot be resolved or is not a field" in the following program:

package com.esri.android.sample;

import android.app.Activity;
import android.os.Bundle;

import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISDynamicMapServiceLayer;

import com.esri.android.R;

public class HelloWorldMapActivity extends Activity {
    MapView map = null;

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Retrieve the map and initial extent from XML layout
        map = (MapView)findViewById(R.id.map);
        // Add dynamic layer to MapView
        map.addLayer(new ArcGISDynamicMapServiceLayer("" +
            "http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"));
        //Retrieve the non-configuration instance data that was previously returned.
        Object init = getLastNonConfigurationInstance();
        if (init != null) {
            map.restoreState((String) init);
        }
    }

    protected void onPause() {
        super.onPause();
        map.pause();
    }

    protected void onResume() {
        super.onResume();
        map.unpause();
    }
}

These errors are shown on the statements setContentView(R.layout.main); and map = (MapView)findViewById(R.id.map);

I do not know why it is showing these errors. Any idea what is the problem in this program?

Try cleaning and rebuilding your project your R (Main resource) file is probably not working right.

Also check if you have a layout named main.xml and a MapView which has id map. And also check if you are importing the correct R file.

 import com.esri.android.R;

This seems suspicious.

I think you need not to use this line:

import com.esri.android.R;

bcoz u just need to add main.xml in res>layout and R.java is automatically included in the generated java files folder.Therefore u do not need to import it using your package name. Similar is the case for mapview.

does this work??

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