Java 我无法让 Google Maps Android API v2 工作:GoogleMap 无法解析为类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19876950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I can't get Google Maps Android API v2 to work : GoogleMap cannot be resolved to a type
提问by mobilGelistirici
I -think I- have done all the steps in these two links :
我 - 认为我 - 已经完成了这两个链接中的所有步骤:
http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/http://developer.android.com/google/play-services/setup.html
http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/ http://developer.android.com/google/play-services/setup.html
so please don't send them as answers.
所以请不要将它们作为答案发送。
I have downloaded the Maps API, copied it into Eclipse Workspace, marked it as library, referenced it in my project. But no success. I'm still getting the errors :
我已经下载了 Maps API,将其复制到 Eclipse Workspace 中,将其标记为库,并在我的项目中引用了它。但没有成功。我仍然收到错误:
GoogleMap cannot be resolved to a type
MapFragment cannot be resolved to a type
GoogleMap 无法解析为类型
MapFragment 无法解析为类型
Here's my MainActivity.Java:
这是我的 MainActivity.Java:
package com.mapsapp.mapsappv1;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends Activity {
// Google Map
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* function to load map. If map is not created it will create it for you
* */
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
}
And here is my AndroidManifest.xml
这是我的 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mapsapp.mapsappv1"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="com.mapsapp.mapsappv1.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.mapsapp.mapsappv1.permission.MAPS_RECEIVE" />
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- this line is part of my trials.. probably unnecessary : -->
<activity android:name="com.google.android.gms.UnusedStub" />
<activity
android:name="com.mapsapp.mapsappv1.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>
<!-- Google Maps API Key -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="myApiKeyGeneratedWithSHA1Key-ItShouldBeTrue" />
</application>
</manifest>
Maybe somebody had this error and knows how to solve it ?
也许有人有这个错误并且知道如何解决它?
and I have tried restarting eclipse, cleaning the project, creating the project from scratch (its actually v10 or something :) ) apparently I'm doing something wrong..
我尝试重新启动 eclipse,清理项目,从头开始创建项目(实际上是 v10 或其他东西:))显然我做错了什么..
Any kind of help will be appreciated. But please don't paste the two links I have mentioned at the beginning.
任何形式的帮助将不胜感激。但是请不要粘贴我开头提到的两个链接。
Edit: Progress Report
编辑:进度报告
@regeme's comment : import com.google.android.gms.maps.GoogleMap;
helped getting rid of the error : GoogleMap cannot be resolved to a type
@regeme 的评论:import com.google.android.gms.maps.GoogleMap;
帮助摆脱错误:GoogleMap cannot be resolved to a type
However, MapFragment error remains.
但是,MapFragment 错误仍然存在。
I have changed the code according @Imtiyaz Khalani's answer. The new code is this :
我已经根据@Imtiyaz Khalani 的回答更改了代码。新代码是这样的:
private void initilizeMap() {
if (mMap == null) {
//googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
// check if map is created successfully or not
if (mMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
But it gives the error : The method getSupportFragmentManager() is undefined for the type MainActivity
但它给出了错误: The method getSupportFragmentManager() is undefined for the type MainActivity
回答by Imtiyaz Khalani
replace
代替
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
to
到
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
回答by Thiago Souto
When this error happens, it is because the imports didn't work well. Let's try this, select the Project with Right click and then select Properties. now u go Android -> Library -> add . select google-play-services_lib .
发生此错误时,是因为导入工作不正常。让我们试试这个,右键单击选择项目,然后选择属性。现在你去 Android -> 图书馆 -> 添加 . 选择 google-play-services_lib 。
obs: u have to import the google_play_services_lib from your sdk directory to your workspace!
obs:您必须将 google_play_services_lib 从您的 sdk 目录导入到您的工作区!
directory : \sdk\extras\google\google_play_services.
目录:\sdk\extras\google\google_play_services。
please tell me if it worked.
请告诉我它是否有效。
Sorry for this horrible grammar(english isn't my native language).
抱歉这个可怕的语法(英语不是我的母语)。
回答by Emil Adz
If you are getting the following errors, it's means that the GoogleMap
class and the MapFragment
class can't be found.
如果您收到以下错误,则表示找不到GoogleMap
类和MapFragment
类。
As those class are part of the google-play-service
library, you have some problem with the way to reference it.
由于这些类是google-play-service
库的一部分,因此引用它的方式存在一些问题。
You can go over the first three steps of this guide I wrote:
您可以查看我写的本指南的前三个步骤:
and make sure you are making it right.
并确保你做对了。
回答by cassioso
Use FragmentActivity instead of Activity.
使用 FragmentActivity 而不是 Activity。
http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html
http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html
回答by Kris
import com.google.android.gms.maps.MapFragment; import com.google.android.gms.maps.GoogleMap;
导入 com.google.android.gms.maps.MapFragment; 导入 com.google.android.gms.maps.GoogleMap;
Manually import the above statement your problem will be solved.
手动导入上面的语句你的问题就解决了。
回答by user2506891
Your original mapFragment should work just fine after importing the two packages mentioned earlier however you manifest is missing:
导入前面提到的两个包后,您的原始 mapFragment 应该可以正常工作,但是您的清单丢失了:
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />