java 无法找到类 com.google.android.gms.maps.SupportMapFragment
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25839336/
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
Could Not Find Class com.google.android.gms.maps.SupportMapFragment
提问by Bryan Muscedere
So I'm working on an app that uses Google Maps v2 API to display a map overlay. I know this question has been asked before but I've tried everything. I am testing my app using a Nexus 5 device that is not rooted and has Google Play Services installed.
所以我正在开发一个使用 Google Maps v2 API 来显示地图叠加层的应用程序。我知道以前有人问过这个问题,但我已经尝试了一切。我正在使用未植根且安装了 Google Play 服务的 Nexus 5 设备测试我的应用程序。
LogCat displays the following:
LogCat 显示以下内容:
09-14 20:18:27.646: E/dalvikvm(10951): Could not find class 'com.google.android.gms.maps.SupportMapFragment', referenced from method com.bryansworld.algonquinreview.MainActivity.setUpMapInitial
09-14 20:18:27.686: E/AndroidRuntime(10951): FATAL EXCEPTION: main
09-14 20:18:27.686: E/AndroidRuntime(10951): Process: com.bryansworld.algonquinreview, PID: 10951
09-14 20:18:27.686: E/AndroidRuntime(10951): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bryansworld.algonquinreview/com.bryansworld.algonquinreview.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
09-14 20:18:27.686: E/AndroidRuntime(10951): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
09-14 20:18:27.686: E/AndroidRuntime(10951): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
09-14 20:18:27.686: E/AndroidRuntime(10951): at android.app.ActivityThread.access0(ActivityThread.java:135)
09-14 20:18:27.686: E/AndroidRuntime(10951): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-14 20:18:27.686: E/AndroidRuntime(10951): at android.os.Handler.dispatchMessage(Handler.java:102)
09-14 20:18:27.686: E/AndroidRuntime(10951): at android.os.Looper.loop(Looper.java:136)
09-14 20:18:27.686: E/AndroidRuntime(10951): at android.app.ActivityThread.main(ActivityThread.java:5001)
09-14 20:18:27.686: E/AndroidRuntime(10951): at java.lang.reflect.Method.invokeNative(Native Method)
09-14 20:18:27.686: E/AndroidRuntime(10951): at java.lang.reflect.Method.invoke(Method.java:515)
09-14 20:18:27.686: E/AndroidRuntime(10951): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
09-14 20:18:27.686: E/AndroidRuntime(10951): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
09-14 20:18:27.686: E/AndroidRuntime(10951): at dalvik.system.NativeStart.main(Native Method)
09-14 20:18:27.686: E/AndroidRuntime(10951): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
09-14 20:18:27.686: E/AndroidRuntime(10951): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
09-14 20:18:27.686: E/AndroidRuntime(10951): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
09-14 20:18:27.686: E/AndroidRuntime(10951): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
09-14 20:18:27.686: E/AndroidRuntime(10951): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
09-14 20:18:27.686: E/AndroidRuntime(10951): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
09-14 20:18:27.686: E/AndroidRuntime(10951): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
09-14 20:18:27.686: E/AndroidRuntime(10951): at android.app.Activity.setContentView(Activity.java:1929)
09-14 20:18:27.686: E/AndroidRuntime(10951): at com.bryansworld.algonquinreview.MainActivity.onCreate(MainActivity.java:33)
09-14 20:18:27.686: E/AndroidRuntime(10951): at android.app.Activity.performCreate(Activity.java:5231)
09-14 20:18:27.686: E/AndroidRuntime(10951): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-14 20:18:27.686: E/AndroidRuntime(10951): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
09-14 20:18:27.686: E/AndroidRuntime(10951): ... 11 more
09-14 20:18:27.686: E/AndroidRuntime(10951): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: make sure class name exists, is public, and has an empty constructor that is public
09-14 20:18:27.686: E/AndroidRuntime(10951): at android.app.Fragment.instantiate(Fragment.java:597)
09-14 20:18:27.686: E/AndroidRuntime(10951): at android.app.Fragment.instantiate(Fragment.java:561)
09-14 20:18:27.686: E/AndroidRuntime(10951): at android.app.Activity.onCreateView(Activity.java:4778)
09-14 20:18:27.686: E/AndroidRuntime(10951): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
09-14 20:18:27.686: E/AndroidRuntime(10951): ... 21 more
09-14 20:18:27.686: E/AndroidRuntime(10951): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.SupportMapFragment" on path: DexPathList[[zip file "/data/app/com.bryansworld.algonquinreview-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.bryansworld.algonquinreview-1, /vendor/lib, /system/lib]]
09-14 20:18:27.686: E/AndroidRuntime(10951): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
09-14 20:18:27.686: E/AndroidRuntime(10951): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
09-14 20:18:27.686: E/AndroidRuntime(10951): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
09-14 20:18:27.686: E/AndroidRuntime(10951): at android.app.Fragment.instantiate(Fragment.java:583)
09-14 20:18:27.686: E/AndroidRuntime(10951): ... 24 more
Basically, it says that it cannot find class com.google.android.gms.maps.SupportMapFragment
. Although I am sure I referenced Google Play Services in my main project.
基本上,它说它找不到 class com.google.android.gms.maps.SupportMapFragment
。虽然我确定我在我的主要项目中引用了 Google Play 服务。
The following image shows that I referenced Google Play Services in my project:
下图显示了我在我的项目中引用了 Google Play 服务:
Also, here is my ApplicationManifest.xml
:
另外,这是我的ApplicationManifest.xml
:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bryansworld.algonquinreview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<!-- Permissions for operation -->
<permission android:name="com.bryansworld.algonquinreview.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
<uses-permission android:name="com.bryansworld.algonquinreview.package.permission.MAPS_RECEIVE"/>
<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" />
<!-- 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" >
<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>
<!-- Google API Key -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="KEY_GOES_HERE" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
Finally, here is my MainActivity.java
(So far, it has been copied from the Google API demo):
最后,这是我的MainActivity.java
(到目前为止,它已从 Google API 演示中复制):
package com.bryansworld.algonquinreview;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.TileOverlay;
import com.google.android.gms.maps.model.TileOverlayOptions;
import com.google.android.gms.maps.model.TileProvider;
import com.google.android.gms.maps.model.UrlTileProvider;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Locale;
public class MainActivity extends FragmentActivity {
//The Algonquin Map URL
private static final String ALGONQUIN_URL_FORMAT =
"http://www.muscedere.ca/algonquin_map/%d/%d/%d.png";
//Map Elements
private GoogleMap mMap;
private TileOverlay mTiles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpMapInitial();
}
@Override
protected void onResume() {
super.onResume();
setUpMapInitial();
}
private void setUpMapInitial(){
//Check to ensure map isn't already set up.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
//Now, we see if we are successful.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.setMapType(GoogleMap.MAP_TYPE_NONE);
TileProvider tileProvider = new UrlTileProvider(256, 256) {
@Override
public synchronized URL getTileUrl(int x, int y, int zoom) {
// The moon tile coordinate system is reversed. This is not normal.
int reversedY = (1 << zoom) - y - 1;
String s = String.format(Locale.US, ALGONQUIN_URL_FORMAT, zoom, x, reversedY);
URL url = null;
try {
url = new URL(s);
} catch (MalformedURLException e) {
throw new AssertionError(e);
}
return url;
}
};
mTiles = mMap.addTileOverlay(new TileOverlayOptions().tileProvider(tileProvider));
}
public void setFadeIn(View v) {
if (mTiles == null) {
return;
}
mTiles.setFadeIn(((CheckBox) v).isChecked());
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
If anyone has any idea, that would be great! Pretty confused right now.
如果有人有任何想法,那就太好了!现在很迷茫。
采纳答案by ashoke
I do not see you mention adding the Android support library
android-support-v4.jar
. You need to make sure Eclipse is properly including it forcom.google.android.gms.maps.SupportMapFragment
to work:Another potential issue is mismatch of play services library in your app and the play services verion on your nexus 5 device. If you downloaded the latest library from SDK manager, it may be higher version than the one on your device. I am guessing your play services version on device is 5.0.89, you can check on device
Settings -> Apps -> Downloaded -> Google Play Services
. Try using older version of the library if that's the case.
我没有看到您提到添加 Android 支持库
android-support-v4.jar
。您需要确保 Eclipse 正确包含它com.google.android.gms.maps.SupportMapFragment
才能工作:另一个潜在问题是您的应用程序中的播放服务库与您的 nexus 5 设备上的播放服务版本不匹配。如果您从 SDK 管理器下载了最新的库,它可能比您设备上的版本更高。我猜您在设备上的播放服务版本是 5.0.89,您可以在设备上查看
Settings -> Apps -> Downloaded -> Google Play Services
。如果是这种情况,请尝试使用旧版本的库。
回答by Sasa Sekulic
Have you tried this suggestion: Unable instantiate android.gms.maps.MapFragment?
您是否尝试过以下建议:无法实例化 android.gms.maps.MapFragment?
"Try right-clicking on your project in Eclipse, select Android Tools > Add Support Library. In my case it was a stale version taken from ActionBarSherlock."
“尝试在 Eclipse 中右键单击您的项目,选择 Android 工具 > 添加支持库。在我的情况下,它是从 ActionBarSherlock 获取的陈旧版本。”
回答by Abhilash Asok
When importing the Google play service-lib, did you add it into the work space. try adding it in to the workspace
导入google play service-lib的时候,有没有加到工作区。尝试将其添加到工作区