3

我正在尝试在我的 android 应用程序上使用 Google maps v2。我正在使用 Android Studio;我按照此处指示的步骤操作:https ://developers.google.com/maps/documentation/android/start#overview

在我的 AndroidManifest.xml 我有:

 <uses-sdk
    android:minSdkVersion="12"
    android:targetSdkVersion="16" />

<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" />
<!-- The following two permissions are not required to use
     Google Maps Android API v2, but are recommended. -->
<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" />

在我的 build.gradle 我有:

android {
compileSdkVersion 17
buildToolsVersion "17.0.0"

defaultConfig {
    minSdkVersion 12
    targetSdkVersion 16
 }
 }

 dependencies {
  compile 'com.android.support:support-v4:18.0.0'
  compile 'com.google.android.gms:play-services:3.1.36'
}

我的第一个活动:

public class HomeActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);
}

}

我的布局:

<?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" />

我可以编译我的项目,但是当我尝试部署到我的手机时,我遇到了这个异常:

09-03 17:28:08.604    9787-9787/? E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{it.mobile/it.mobile.HomeActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
    at android.app.ActivityThread.access$600(ActivityThread.java:149)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:153)
    at android.app.ActivityThread.main(ActivityThread.java:5086)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:258)
    at android.app.Activity.setContentView(Activity.java:1867)
    at it.mobile.HomeActivity.onCreate(HomeActivity.java:12)
    at android.app.Activity.performCreate(Activity.java:5020)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
    ... 11 more
    Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public
    at android.app.Fragment.instantiate(Fragment.java:584)
    at android.app.Fragment.instantiate(Fragment.java:552)
    at android.app.Activity.onCreateView(Activity.java:4668)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
    ... 20 more
    Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.MapFragment
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
    at android.app.Fragment.instantiate(Fragment.java:574)
    ... 23 more

在项目的属性中,我检查了导出以支持库和谷歌服务库。

谢谢

4

1 回答 1

0

At the end I solved the problem recreating a new project in Android Studio. Not sure which was the problem.

于 2013-09-03T16:52:44.377 回答