入门"如谷歌和QUOT说在Android的工作室无法添加地图;页; Android的工作室提供了错误 [英] Cannot add map in Android Studio as stated in Google "Getting Started" page; Android Studio gives error
问题描述
我试图按照指示,以与谷歌地图API的应用程序。要做到这一点,我下面的说明严格按描述这里 :我已经有机器人工作室,并参与了几个非常基本的联系人列表中的应用程序,但是为了这个,我得到了API密钥,设置的位置和网络的权限,并设置了OpenGL ES的V2。 我复制了code右下面两节从网站上,贴到活动.xml文件和Java类。
I'm trying to follow the directions to make an app with the Google Maps API. To do this, I'm following the instructions exactly as described here: I already had Android Studio and worked on a couple of very elementary Contact List apps, but for this I got the API key, set up the location and network permissions and set up OpenGL ES v2. I copied the following two sections of code right from the website and pasted it to the activity .xml file and the java class.
<?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"/>
到布局XML文件,下面的MainActivity.java文件。
to the layout XML file and the following to the MainActivity.java file.
package com.example.mapdemo;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
我碰上与code中的第一个块的一个问题。 Android的工作室返回 android.gms.maps.MapFragment
为错误,通过突出显示为红色。这也给了我一个错误的研究
的所有使用实例... 研究
的任何使用以红色突出显示。我试图导入 android.R
,但没有奏效。但我想一个问题的时间。
I run into a problem with the first block of code. Android Studio returns the "android.gms.maps.MapFragment
" as an error, by highlighting it red. It also gives me an error for all usages of R
... any usage of R
is highlighted in red. I tried importing android.R
but didn't work. But I guess one problem at a time.
任何想法?谢谢!
推荐答案
编辑:我刚开始一个新的项目,并随后从该链接的说明。它为我工作。
有一点要注意的是,我没有改变任何东西 MainActivity.java
。
I just started a new project and followed the instructions from that link. It worked for me.
One thing to note is that I didn't have to change anything in MainActivity.java
.
我会建议开始在Android的Studio中的一个新的项目,选择空白活动。
修改的AndroidManifest.xml
, build.gradle
和 activity_main.xml
,但一切都留在 MainActivity.java
原样。
I would recommend starting a new project in Android Studio, select "Blank Activity".
Modify AndroidManifest.xml
, build.gradle
, and activity_main.xml
, but leave everything in MainActivity.java
as is.
有一点需要注意的是,从SDK管理器中,您需要安装:
One thing to note is that from the SDK Manager, you need to install:
- 在Android的支持库
- 在Android的支持库
- 在谷歌播放服务
- 在谷歌信息库
这是我的工作的例子:
在build.gradle:
In build.gradle:
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中:
AndroidManifest.xml:
<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>
我得到了它设置了一个片段
在 MainActivity
。
I got it set up with a Fragment
within MainActivity
.
activity_main.xml(没有什么特别的位置):
activity_main.xml (nothing special here):
<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" />
fragment_main.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" />
MainActivity.java:
MainActivity.java:
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();
}
}
}
结果:
这篇关于入门&QUOT;如谷歌和QUOT说在Android的工作室无法添加地图;页; Android的工作室提供了错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!