Java 无法在Android Studio中添加地图,如Google中所述;“入门”;页Android Studio给出了错误消息
我正试图按照说明使用谷歌地图API制作一个应用程序。为了做到这一点,我严格按照说明进行操作:我已经有了Android Studio,并开发了两个非常基本的联系人列表应用程序,但为此我获得了API密钥,设置了位置和网络权限,并设置了OpenGL ES v2。 我从网站上复制了以下两部分代码,并将其粘贴到activity.xml文件和java类中Java 无法在Android Studio中添加地图,如Google中所述;“入门”;页Android Studio给出了错误消息,java,android,google-maps,android-studio,Java,Android,Google Maps,Android Studio,我正试图按照说明使用谷歌地图API制作一个应用程序。为了做到这一点,我严格按照说明进行操作:我已经有了Android Studio,并开发了两个非常基本的联系人列表应用程序,但为此我获得了API密钥,设置了位置和网络权限,并设置了OpenGL ES v2。 我从网站上复制了以下两部分代码,并将其粘贴到activity.xml文件和java类中 <?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
我遇到了第一个代码块的问题。Android Studio将“Android.gms.maps.MapFragment
”突出显示为红色,作为错误返回。它还为R
的所有用法提供了一个错误。。。R
的任何用法都以红色突出显示。我试着导入android.R,但没有成功。但我想一次只能解决一个问题
有什么想法吗?谢谢 编辑:我刚刚开始了一个新项目,并按照链接中的说明进行操作。这对我有用。 需要注意的一点是,我不必在
MainActivity.java
中更改任何内容
我建议在Android Studio中启动一个新项目,选择“空白活动”。
修改AndroidManifest.xml
、build.gradle
和activity\u main.xml
,但保持MainActivity.java
中的所有内容不变
需要注意的一点是,在SDK管理器中,您需要安装:
- Android支持库
- Android支持库
- 谷歌游戏服务
- 谷歌知识库
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.google.android.gms:play-services-maps:7.0.0'
}
AndroidManifest.xml:
fragment_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
结果:
您是否将google play服务添加到依赖项中?一旦地图问题得到解决,R问题就应该得到解决。实际上我没有。所以我在build.gradle中添加了一行:“编译'com.google.android.gms:play services:7.0.0'”。MapFragment错误消失了。但是我仍然有R类错误。这使我无法编译和运行该应用程序。自从包含依赖项后,你是否进行了清理和重建?是的,不起作用。错误依然存在。您是否仍在导入android.R?如果是这样,请删除该导入语句。我确实从原始下载中安装了所有这些语句。我只是检查并更新了它们。再次清理和重建项目。没有更改。谢谢,我认为问题出在一些重复的代码行中。我不得不重新排列XML文件中的一些导入行。没有错误了。我不得不等待,看看代码是否真的做了它应该做的,因为我现在没有USB电缆在我的手机上测试。。。虚拟设备要求我安装Google Play服务,我已经安装了。
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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="*********************"/>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity" tools:ignore="MergeRootFrame" />
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.view.Menu;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.location.Location;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
MapView mMapView;
private GoogleMap googleMap;
Location location;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflate and return the layout
View v = inflater.inflate(R.layout.fragment_main, container,
false);
mMapView = (MapView) v.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();// needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
googleMap = mMapView.getMap();
googleMap.setMyLocationEnabled(true);
location = googleMap.getMyLocation();
if (location != null) {
// latitude and longitude
LatLng lat = new LatLng(location.getLatitude(), location.getLongitude());
//double latitude = 17.385044;
//double longitude = 78.486671;
//LatLng lat = new LatLng(latitude,longitude);
// create marker
MarkerOptions marker = new MarkerOptions().position(
lat).title("Hello Maps");
// Changing marker icon
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(lat).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
// Perform any camera updates here
return v;
}
@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
}
}