Google Maps v2 XML错误和R无法解析为变量

时间:2014-01-24 07:27:50

标签: android xml google-maps

我正在尝试使用gps创建一个谷歌地图,随时随地显示用户位置。在我的xml文件中,出现错误“解析XML错误:XML声明格式不正确”,并且在我的MainActivity java文件中,“R无法解析为变量”,首先没有这样的错误,但有一次我清理我的项目然后出现错误,永远不会消失。我尝试删除行“”但它会导致更多错误。 然后,每次我保存我的项目,除了前两个之外,还会有两个错误。 “意外的名称空间前缀”xmlns“找到标签片段”和“XML版本”1.0 encoding =“不支持,XML中也只支持XML 1.0”。在我清理mt项目之后,最后两个将会消失一段时间,直到我再次保存 我听说XML会影响R文件,但我不确定在这种情况下导致R的原因。

XML文件

<?xml version="1.0 encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<fragment
    android:name="com.google.android.gms.maps.SupportMapFragment"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent" /> 
</LinearLayout>

android清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.studenthealthapp"
android:versionCode="1"
android:versionName="1.0" >

<permission android:name="your.application.package.permission.MAPS_RECEIVE"    android:protectionLevel="signature" />

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<uses-features
    android:glEsVersion="0x00020000"
    android:required="true"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.studenthealthapp.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="API key value" />

</application>

主Java文件

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.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {

private GoogleMap googleMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setUpMapIfNeeded();
}

private void setUpMapIfNeeded() {

    if(googleMap == null)
    {
        googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

        if(googleMap != null)
        {
            setUpMap();
        }
    }

}

private void setUpMap() 
{
    //Enable MyLocation Layer of Google Map
    googleMap.setMyLocationEnabled(true);

    //Get locationManager object from System Service LOCATION_SERVICE
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    //Create a criteria object to retrieve provider
    Criteria criteria = new Criteria();

    //Get the name of the best provider
    String provider = locationManager.getBestProvider(criteria, true);

    //Get current location
    Location myLocation = locationManager.getLastKnownLocation(provider);

    //set map type
    googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

    //Get latitude of the current location
    double latitude = myLocation.getLatitude();

    //Get longitude of the current location
    double longitude = myLocation.getLongitude();

    //Create a LatLng object for the current location
    LatLng latLng = new LatLng(latitude, longitude);

    //Show the current location in Google Map
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

    //Zoom in the Google Map
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(20));
    googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!"));

}

@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);
    return true;
}}

我知道Google Map API v1无法使用,而我正在使用的v2需要连接到真正的Android设备。任何人都可以为我确认这张脸吗?因为我的应用程序有很多其他部分不需要谷歌地图所以我们如何测试我们的应用程序?一些在Android手机上的模拟器上?由于错误,我还没有在我的手机上测试过,所以我想我只需要将笔记本电脑连接到手机并运行,同时选择设备?我会感激任何帮助。感谢

1 个答案:

答案 0 :(得分:0)

删除此

 xmlns:map="http://schemas.android.com/apk/res-auto"

表示片段

同时更改

<?xml version="1.0 encoding="utf-8"?>

<?xml version="1.0" encoding="utf-8"?>

如果您的任何资源文件中有错误,则不会生成R.java。因此,请确保资源文件中没有错误。清理并建立项目。

在Google Play服务更新第13版之后,您还缺少清单的应用标记,这是新要求。

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />