2

我无法从开发人员网站上获取此示例并且卡住了(可能是一些简单的事情)这是我的 xml 布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity" >

 <fragment 
        android:id="@+id/map"
        class="com.google.android.gms.maps.MapFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</RelativeLayout>

我的主要活动

    package PackageName.AndroidMapsTest;
    import android.os.Bundle;
    import android.app.Activity;

    public class MainActivity extends Activity {
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.map_test_1);
        }
    }

和我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="Pakagename.AndroidMapsTest"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <permission
        android:name="Pakagename.AndroidMapsTest.permission.MAPS_RECIVE"
        android:protectionLevel="signature" />
        <uses-permission android:name="Pakagename.AndroidMapsTest.permission.MAPS_RECIVE"/>

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

    <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="edu.lewisu.cs.shanejmiller.AndroidMapsTest.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="my_api_key" />

    </application>

</manifest>

我从日志猫得到的错误是

java.lang.RunTimeException: Unable to start activity ComponentInfo{packagename.AndroidMapsTest/packagename.AndroidMapsTest.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment 

我现在被困了一段时间,希望能得到任何帮助。提前致谢

日志跟踪

07-05 22:00:55.633: E/AndroidRuntime(1799): FATAL EXCEPTION: main
07-05 22:00:55.633: E/AndroidRuntime(1799): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.lewisu.cs.shanejmiller.AndroidMapsTest/edu.lewisu.cs.shanejmiller.AndroidMapsTest.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
07-05 22:00:55.633: E/AndroidRuntime(1799):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-05 22:00:55.633: E/AndroidRuntime(1799):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-05 22:00:55.633: E/AndroidRuntime(1799):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-05 22:00:55.633: E/AndroidRuntime(1799):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-05 22:00:55.633: E/AndroidRuntime(1799):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 22:00:55.633: E/AndroidRuntime(1799):     at android.os.Looper.loop(Looper.java:137)
07-05 22:00:55.633: E/AndroidRuntime(1799):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-05 22:00:55.633: E/AndroidRuntime(1799):     at java.lang.reflect.Method.invokeNative(Native Method)
07-05 22:00:55.633: E/AndroidRuntime(1799):     at java.lang.reflect.Method.invoke(Method.java:511)
07-05 22:00:55.633: E/AndroidRuntime(1799):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-05 22:00:55.633: E/AndroidRuntime(1799):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-05 22:00:55.633: E/AndroidRuntime(1799):     at dalvik.system.NativeStart.main(Native Method)
07-05 22:00:55.633: E/AndroidRuntime(1799): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
07-05 22:00:55.633: E/AndroidRuntime(1799):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
07-05 22:00:55.633: E/AndroidRuntime(1799):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
07-05 22:00:55.633: E/AndroidRuntime(1799):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-05 22:00:55.633: E/AndroidRuntime(1799):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-05 22:00:55.633: E/AndroidRuntime(1799):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-05 22:00:55.633: E/AndroidRuntime(1799):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
07-05 22:00:55.633: E/AndroidRuntime(1799):     at android.app.Activity.setContentView(Activity.java:1881)
07-05 22:00:55.633: E/AndroidRuntime(1799):     at edu.lewisu.cs.shanejmiller.AndroidMapsTest.MainActivity.onCreate(MainActivity.java:15)
07-05 22:00:55.633: E/AndroidRuntime(1799):     at android.app.Activity.performCreate(Activity.java:5104)
07-05 22:00:55.633: E/AndroidRuntime(1799):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-05 22:00:55.633: E/AndroidRuntime(1799):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-05 22:00:55.633: E/AndroidRuntime(1799):     ... 11 more
07-05 22:00:55.633: E/AndroidRuntime(1799): 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
07-05 22:00:55.633: E/AndroidRuntime(1799):     at android.app.Fragment.instantiate(Fragment.java:592)
07-05 22:00:55.633: E/AndroidRuntime(1799):     at android.app.Fragment.instantiate(Fragment.java:560)
07-05 22:00:55.633: E/AndroidRuntime(1799):     at android.app.Activity.onCreateView(Activity.java:4709)
07-05 22:00:55.633: E/AndroidRuntime(1799):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
07-05 22:00:55.633: E/AndroidRuntime(1799):     ... 21 more
07-05 22:00:55.633: E/AndroidRuntime(1799): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.MapFragment" on path: /data/app/edu.lewisu.cs.shanejmiller.AndroidMapsTest-2.apk
07-05 22:00:55.633: E/AndroidRuntime(1799):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
07-05 22:00:55.633: E/AndroidRuntime(1799):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
07-05 22:00:55.633: E/AndroidRuntime(1799):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
07-05 22:00:55.633: E/AndroidRuntime(1799):     at android.app.Fragment.instantiate(Fragment.java:582)
07-05 22:00:55.633: E/AndroidRuntime(1799):     ... 24 more
4

3 回答 3

4

您正在尝试将com.google.android.gms.maps.MapFragment(对于原生 API 级别 11+ 片段)与FragmentActivity(对于片段的 Android 支持反向移植)一起使用。那些需要匹配。由于您android:minSdkVersion12 岁,请更改FragmentActivityActivity.

于 2013-07-05T20:07:37.810 回答
1

尝试将其添加到清单中:

<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

这个对我有用。

于 2013-12-05T10:49:18.587 回答
1
  <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value=" your api key" />

在这下面也包括这个

        <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

并像这样更改xml....

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />

</RelativeLayout>

将活动更改为

 public class MainActivity extends FragmentActivity 

并初始化为.......

 mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();

应该这样做。

于 2014-01-02T12:19:50.333 回答