我是Stackoverflow的新手。我在新版Google Maps API v2上替换地图时遇到问题。 我完成了本页的所有步骤https://developers.google.com/maps/documentation/android/start?hl=pl和一些教程,我无法显示地图。当我运行我的应用程序时,在模拟器上显示只有放大控件和休息屏幕是白色的我无法在我的真实设备上打开它(HTC Wildfire S- android 2.3.5)。我也将google_play_service_lib添加到了我的项目中。我在很多页面和很多教程中寻找我的问题的答案,但我没有找到它!请帮帮我!
这是我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.adrianspora.googlemaptest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.adrianspora.googlemaptest.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="AIzaSyD6r1gse4Jx4sTrCECvLQQwgxLJikHm4Ow"/>
</application>
<permission
android:name="com.adrianspora.googlemaptest.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.adrianspora.googlemaptest.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<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-feature
android:glEsVersion="0x00020000"
android:required="true"/>
</manifest>
有MainActivity的代码:
package com.adrianspora.googlemaptest;
import com.google.android.gms.maps.MapFragment;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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;
}
}
这是主要布局的代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
</LinearLayout>
LogCat中显示了一系列日志:
06-22 14:51:44.897: D/AndroidRuntime(1092): Shutting down VM
06-22 14:51:44.927: W/dalvikvm(1092): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-22 14:51:44.996: E/AndroidRuntime(1092): FATAL EXCEPTION: main
06-22 14:51:44.996: E/AndroidRuntime(1092): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.adrianspora.googlemaptest/com.adrianspora.googlemaptest.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
06-22 14:51:44.996: E/AndroidRuntime(1092): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-22 14:51:44.996: E/AndroidRuntime(1092): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-22 14:51:44.996: E/AndroidRuntime(1092): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-22 14:51:44.996: E/AndroidRuntime(1092): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-22 14:51:44.996: E/AndroidRuntime(1092): at android.os.Handler.dispatchMessage(Handler.java:99)
06-22 14:51:44.996: E/AndroidRuntime(1092): at android.os.Looper.loop(Looper.java:137)
06-22 14:51:44.996: E/AndroidRuntime(1092): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-22 14:51:44.996: E/AndroidRuntime(1092): at java.lang.reflect.Method.invokeNative(Native Method)
06-22 14:51:44.996: E/AndroidRuntime(1092): at java.lang.reflect.Method.invoke(Method.java:511)
06-22 14:51:44.996: E/AndroidRuntime(1092): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-22 14:51:44.996: E/AndroidRuntime(1092): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-22 14:51:44.996: E/AndroidRuntime(1092): at dalvik.system.NativeStart.main(Native Method)
06-22 14:51:44.996: E/AndroidRuntime(1092): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
06-22 14:51:44.996: E/AndroidRuntime(1092): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
06-22 14:51:44.996: E/AndroidRuntime(1092): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
06-22 14:51:44.996: E/AndroidRuntime(1092): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-22 14:51:44.996: E/AndroidRuntime(1092): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-22 14:51:44.996: E/AndroidRuntime(1092): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-22 14:51:44.996: E/AndroidRuntime(1092): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
06-22 14:51:44.996: E/AndroidRuntime(1092): at android.app.Activity.setContentView(Activity.java:1881)
06-22 14:51:44.996: E/AndroidRuntime(1092): at com.adrianspora.googlemaptest.MainActivity.onCreate(MainActivity.java:15)
06-22 14:51:44.996: E/AndroidRuntime(1092): at android.app.Activity.performCreate(Activity.java:5104)
06-22 14:51:44.996: E/AndroidRuntime(1092): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-22 14:51:44.996: E/AndroidRuntime(1092): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-22 14:51:44.996: E/AndroidRuntime(1092): ... 11 more
06-22 14:51:44.996: E/AndroidRuntime(1092): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
06-22 14:51:44.996: E/AndroidRuntime(1092): at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
06-22 14:51:44.996: E/AndroidRuntime(1092): at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
06-22 14:51:44.996: E/AndroidRuntime(1092): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
06-22 14:51:44.996: E/AndroidRuntime(1092): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
06-22 14:51:44.996: E/AndroidRuntime(1092): ... 21 more
06-22 15:00:25.848: D/dalvikvm(1273): GC_CONCURRENT freed 256K, 15% free 2600K/3032K, paused 85ms+37ms, total 193ms
06-22 15:00:26.236: D/dalvikvm(1273): GC_CONCURRENT freed 272K, 14% free 2742K/3188K, paused 72ms+5ms, total 116ms
06-22 15:00:26.267: E/Google Maps Android API(1273): Google Maps Android API v2 only supports devices with OpenGL ES 2.0 and above
06-22 15:00:26.487: D/dalvikvm(1273): GC_FOR_ALLOC freed 152K, 11% free 2895K/3224K, paused 40ms, total 41ms
06-22 15:00:26.747: D/dalvikvm(1273): GC_CONCURRENT freed 85K, 8% free 3271K/3540K, paused 22ms+87ms, total 155ms
06-22 15:00:27.057: D/libEGL(1273): Emulator without GPU support detected. Fallback to software renderer.
06-22 15:00:27.087: I/Choreographer(1273): Skipped 56 frames! The application may be doing too much work on its main thread.
06-22 15:00:27.127: D/libEGL(1273): loaded /system/lib/egl/libGLES_android.so
06-22 15:00:27.188: D/gralloc_goldfish(1273): Emulator without GPU emulation detected.
06-22 15:00:28.117: D/dalvikvm(1273): GC_CONCURRENT freed 266K, 12% free 3470K/3912K, paused 36ms+7ms, total 128ms
06-22 15:00:28.117: D/dalvikvm(1273): WAIT_FOR_CONCURRENT_GC blocked 79ms
06-22 15:00:28.216: D/dalvikvm(1273): GC_FOR_ALLOC freed 435K, 18% free 3340K/4044K, paused 45ms, total 51ms
06-22 15:00:28.348: D/dalvikvm(1273): GC_CONCURRENT freed 326K, 15% free 3470K/4044K, paused 15ms+8ms, total 69ms
06-22 15:00:28.348: D/dalvikvm(1273): WAIT_FOR_CONCURRENT_GC blocked 45ms
06-22 15:00:28.557: D/dalvikvm(1273): GC_CONCURRENT freed 551K, 19% free 3399K/4152K, paused 21ms+26ms, total 113ms
06-22 15:00:36.126: E/Google Maps Android API(1273): Authorization failure. Please see https://developers.google.com/maps/documentation/android/start for how to correctly set up the map.
06-22 15:00:36.136: E/Google Maps Android API(1273): Ensure that the following correspond to what is in the API Console: Package Name: com.adrianspora.googlemaptest, API Key: AIzaSyD6r1gse4Jx4sTrCECvLQQwgxLJikHm4Ow, Certificate Fingerprint: 1D915CBBCDEBB8CECC48371ED32EE0D5CA5DB443
06-22 15:14:09.636: E/Trace(1502): error opening trace file: No such file or directory (2)
06-22 15:14:11.196: D/dalvikvm(1502): GC_CONCURRENT freed 230K, 14% free 2599K/3004K, paused 124ms+91ms, total 328ms
06-22 15:14:11.386: E/Google Maps Android API(1502): Google Maps Android API v2 only supports devices with OpenGL ES 2.0 and above
06-22 15:14:11.626: D/dalvikvm(1502): GC_CONCURRENT freed 264K, 14% free 2789K/3228K, paused 73ms+86ms, total 226ms
06-22 15:14:11.796: D/dalvikvm(1502): GC_FOR_ALLOC freed 119K, 12% free 2913K/3280K, paused 37ms, total 38ms
06-22 15:14:12.106: D/dalvikvm(1502): GC_CONCURRENT freed 64K, 8% free 3288K/3556K, paused 71ms+73ms, total 208ms
06-22 15:14:12.477: D/libEGL(1502): Emulator without GPU support detected. Fallback to software renderer.
06-22 15:14:12.496: D/libEGL(1502): loaded /system/lib/egl/libGLES_android.so
06-22 15:14:12.536: I/Choreographer(1502): Skipped 69 frames! The application may be doing too much work on its main thread.
06-22 15:14:12.656: D/gralloc_goldfish(1502): Emulator without GPU emulation detected.
06-22 15:14:13.307: D/dalvikvm(1502): GC_CONCURRENT freed 332K, 14% free 3358K/3868K, paused 143ms+137ms, total 477ms
06-22 15:14:13.307: D/dalvikvm(1502): WAIT_FOR_CONCURRENT_GC blocked 322ms
06-22 15:14:13.766: D/dalvikvm(1502): GC_CONCURRENT freed 365K, 15% free 3471K/4040K, paused 6ms+7ms, total 296ms
06-22 15:14:13.766: D/dalvikvm(1502): WAIT_FOR_CONCURRENT_GC blocked 97ms
06-22 15:14:13.866: D/dalvikvm(1502): GC_FOR_ALLOC freed 434K, 18% free 3342K/4040K, paused 39ms, total 46ms
06-22 15:14:14.056: D/dalvikvm(1502): GC_CONCURRENT freed 433K, 16% free 3400K/4040K, paused 12ms+27ms, total 109ms
06-22 15:14:18.076: E/Google Maps Android API(1502): Authorization failure. Please see https://developers.google.com/maps/documentation/android/start for how to correctly set up the map.
06-22 15:14:18.076: E/Google Maps Android API(1502): Ensure that the following correspond to what is in the API Console: Package Name: com.adrianspora.googlemaptest, API Key: AIzaSyD6r1gse4Jx4sTrCECvLQQwgxLJikHm4Ow, Certificate Fingerprint: 1D915CBBCDEBB8CECC48371ED32EE0D5CA5DB443
06-22 15:14:23.066: I/Choreographer(1502): Skipped 96 frames! The application may be doing too much work on its main thread.
答案 0 :(得分:4)
你应该使用Support Fragment
,因为你的min sdk是8。
<fragment
class="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
您的活动必须延长FragmentActivity
并使用以下内容初始化GoogleMap对象。
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
GoogleMap mMap = fm.getMap();
确保您已添加支持库
同时确保导入以下内容
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.SupportMapFragment;
检查您是否在服务标签下的google api控制台中为启用了Android地图。
检查您是否已按照文档中提到的所有步骤进行操作
https://developers.google.com/maps/documentation/android/start
您也可以查看我回答的类似问题。
App shows grid screen instead of google map
当我运行我的应用程序时,在模拟器上只显示放大率 控件和休息屏幕是白色的
您应该在真实设备上进行测试。
答案 1 :(得分:0)
您应该使用SupportMapFragment,因为您的API级别是2.3.5
使用以下代码替换XML中的片段:
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
并且还在Java文件中导入此类,而不是MapFragment。
此外,作为一般建议,如果您想支持蜂窝前蜂窝设备并使用碎片等,则应使用Support library。
答案 2 :(得分:0)
您使用的是哪种API密钥?您应该在编程时始终使用开发键,并且您的应用需要在Google API控制台中注册。
您必须在偏好设置/ Android 中添加google-play-services-library,而不是偏好设置/ Java构建路径。
答案 3 :(得分:0)
You have to use FragmentActivity
public class PlacesMapActivity extends android.support.v4.app.FragmentActivity
implements OnClickListener, LocationListener {
ImageButton btn_home;
private GoogleMap mMap;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
SupportMapFragment fragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mMap = fragment.getMap();
mMap.setMyLocationEnabled(true);
// mMap = ((SupportMapFragment) getSupportFragmentManager()
// .findFragmentById(R.id.map)).getMap();
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.title("First Location");
markerOptions.snippet("This Is Test Location");
LatLng latlng = new LatLng(23.0333, 72.6167);
markerOptions.position(latlng);
// markerOptions.title("Ahmedabad Cordinat Found here");
// Marker m = mMap.addMarker(markerOptions);
mMap.setInfoWindowAdapter(new InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker arg0) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
View myContentView = getLayoutInflater().inflate(
R.layout.custommarker, null);
TextView tvTitle = ((TextView) myContentView
.findViewById(R.id.title));
tvTitle.setText(marker.getTitle());
TextView tvSnippet = ((TextView) myContentView
.findViewById(R.id.snippet));
tvSnippet.setText(marker.getSnippet());
return myContentView;
}
});
mMap.addMarker(new MarkerOptions()
.position(latlng)
.title("This is Sabarmati Ashram")
.snippet("Ahmedabad")
.icon(BitmapDescriptorFactory .defaultMarker(BitmapDescriptorFactory.HUE_RED)));
mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(getBaseContext(),
DetailsOfPlacesActivity.class);
startActivity(intent);
}
});
btn_home = (ImageButton) findViewById(R.id.activity_map_ibtn_home);
btn_home.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
答案 4 :(得分:0)
我遇到了同样的错误并尝试了6个教程,并阅读了谷歌的所有文档。最后,我让它运行并显示地图。这就是它对我有用的方式。
1.从你的eclipse和你的PC上删除库google-play-services和android support-v4。 2.从de sdk-manager再次下载。 3.按照说明[此处](https://developers.google.com/maps/documentation/android/intro#sample_code)转到示例代码并按照说明进行操作。请特别检查此现有Android代码到工作区,不要将库作为项目导入,它们将无法正常工作。 4.更改所有MapFragment for SupportMapFragment,不要忘记将你的Actvity扩展到FragmentActivity(这是我的失败)。 5.Disable(如果已启用)自动构建项目中的选项。 6.清理项目。 5.自动启用构建选项。
它应该有用。