【问题标题】:In Android Project, Single File has R errors [duplicate]在Android项目中,单个文件有R错误[重复]
【发布时间】:2014-02-19 03:54:14
【问题描述】:

我有一个项目可以在 Eclipse 中的 Windows 上完美运行。由于我现在的大部分工作都是在 Mac 上完成的,因此我正在切换到 Mac 版 Eclipse。

我已经修复了整个项目的所有构建路径错误,剩下的就是“this_xml_name cannot be resolve or is not a field”,理论上会导致构建我的 R 文件时出错。

但是,这是我第一次遇到这个问题只出现在单个文件中,而且不仅仅是 id 或布局。它发生在 R.layout、R.id、R.menu 和 R.drawable 中。我项目中的所有其他引用都非常好,在链接文件中的每个 R. 引用中都发现了错误。

MapActivity.java

package com.example.android;

import java.util.Timer;
import java.util.TimerTask;

import android.R;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
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 MapActivity extends FragmentActivity {

        FragmentActivity activity = this;

        private static final int GPS_ERRORDIALOG_REQUEST = 9001;
        protected GoogleMap mMap;
        String locAddr;
        String locName;
        Double locLongitude;
        Double locLatitude;
        final Handler handler = new Handler();


        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);


                if (servicesOK()) {
                        setContentView(R.layout.activity_map);

                        if (initMap()) {
                                Bundle extras = getIntent().getExtras();
                                if (extras != null) {
                                        locAddr = extras.getString("address");
                                        locName = extras.getString("name");
                                        locLongitude = extras.getDouble("longitude");
                                        locLatitude = extras.getDouble("latitude");

                                        activity.setTitle(locName);

                                        Timer timer = new Timer();
                                        timer.schedule(new TimerTask() {

                                                @Override
                                                public void run() {

                                                         handler.post(new Runnable()
                                                    {
                                                        public void run()
                                                        {
                                                                LatLng coordinates = new LatLng(locLatitude, locLongitude);

                                                                                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinates, 14.0f));

                                                                                Marker marker = mMap.addMarker(new MarkerOptions()
                                                                                        .position(coordinates)
                                                                                        .title(locName)
                                                                                        .snippet(locAddr)
                                                                                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.map_marker)));

                                                                                marker.showInfoWindow();
                                                                                /*mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

                                                                                        @Override
                                                                                        public void onInfoWindowClick(Marker arg0) {
                                                                                                Intent intent = new Intent(MapActivity.this, DetailsActivity.class);
                                                                                                intent.putExtra("userkey", "MattCoker");
                                                                                                intent.putExtra("geolat", "29.7530");
                                                                                                intent.putExtra("geolong", "geozip");
                                                                                                intent.putExtra("clientid", "0");
                                                                                                intent.putExtra("siteid", "0");
                                                                                                startActivity(intent);
                                                                                        }
                                                                                });*/
                                                        }
                                                    });

                                                }
                                        }, 3000);


                                } else {
                                        Toast.makeText(activity, "Something went wrong displaying this location!", Toast.LENGTH_LONG).show();
                                }
                        }
                }
                else {
                        setContentView(R.layout.no_available_maps);
                }

                //Show the Up button in the action bar.
                setupActionBar();


        }

        /**
         * Set up the {@link android.app.ActionBar}.
         */
        private void setupActionBar() {

                activity.getActionBar().setDisplayHomeAsUpEnabled(true);

        }

        public boolean servicesOK() {
                int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(activity);

                if (isAvailable == ConnectionResult.SUCCESS) {
                        return true;
                }
                else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
                        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, activity, GPS_ERRORDIALOG_REQUEST);
                        dialog.show();
                }
                else {
                        Toast.makeText(activity, "Can't connect to Google Play Services", Toast.LENGTH_SHORT).show();
                }
                return false;
        }

        private boolean initMap() {
                if (mMap == null) {
                        SupportMapFragment mapFrag = null;
                        mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

                        mMap = mapFrag.getMap();
                }
                return (mMap != null);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.main, menu);

            // Associate searchable configuration with the SearchView
            /*SearchManager searchManager =
                   (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            SearchView searchView =
                    (SearchView) menu.findItem(R.id.action_search).getActionView();
            searchView.setSearchableInfo(
                    searchManager.getSearchableInfo(getComponentName()));*/

            return true;
        }


        @Override
        public boolean onOptionsItemSelected(MenuItem item) {

                int selectedMenuItem = item.getItemId();

                if (selectedMenuItem == R.id.action_settings) {
                        Intent settingsIntent = new Intent(activity, SettingsActivity.class);
                        startActivity(settingsIntent);
                        return true;
                } else if (selectedMenuItem == R.id.send_feedback) {
                        Intent sendFeedback = new Intent(android.content.Intent.ACTION_SEND);
                        sendFeedback.setType("message/rfc822");
                        sendFeedback.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {"usersupport@example.com"});
                        sendFeedback.putExtra(android.content.Intent.EXTRA_SUBJECT, "Feedback on example...");

                        startActivity(Intent.createChooser(sendFeedback, "Send Email"));

                        //TODO: Create Feedback Activity to send email

                        return true;
                }
                return false;
        }

}

【问题讨论】:

  • 删除 R.java 文件并重新构建您的项目。
  • 将 import android.R 替换为 import com.example.android.R;

标签: java android xml eclipse resources


【解决方案1】:

如果您的资源文件中有错误,R.java 将不会生成。修复这些错误并清理和构建。

查看你拥有的代码

import android.R;

替换为

import com.example.android.R;

【讨论】:

    猜你喜欢
    • 2013-04-01
    • 1970-01-01
    • 2014-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-13
    • 2013-06-27
    • 2020-12-12
    相关资源
    最近更新 更多