android - 二进制 XML 文件行 #8 : Error inflating class fragment, Google map

标签 android xml mapfragment

在这个应用程序中,我试图添加一个功能性的 V2 谷歌地图。我已经查看了大多数关于创建 Activity 、获取 map key 和创建 xml 文件的教程,以尝试查看我是否错过了任何步骤(我经历了 4 次),但它没有好像我错过了任何一步。

Here是我使用的网站:

当我在手机上运行我的应用程序时(因为 Google Play 服务不能在模拟器上运行),它给我以下两个错误:

  02-11 22:10:54.690: E/AndroidRuntime(4526): java.lang.RuntimeException: Unable to       start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.ShowActivity}:       android.view.InflateException: Binary XML file line #8: Error inflating class fragment
  02-11 22:10:54.690: E/AndroidRuntime(4526): 

  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

这是我的 Activity :

package com.example.myfirstapp;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import com.google.android.maps.*;
import com.google.android.gms.maps.MapFragment;
public class ShowActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_map, menu);
    return true;
}

//@Override
//protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    //return false;
//}

    }

抱歉格式化,但这是我的 xml 文件:

<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=".ShowActivity" >

<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.MapFragment" />

  </RelativeLayout>

我已经导入了 Google API 和 google play 服务,所以所有的导入都在工作,这让我想知道为什么第二个错误说 MapFragment 是空的。 list 文件代码:

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

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.SUBSCRIBED_FEEDS_READ" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission        android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.example.myfirstapp.permission.MAPS_RECEIVE" />
<permission
    android:name="com.example.myfirstapp.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.myfirstapp.MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <meta-data
            android:name="android.app.default_searchable"
            android:value=".SearchResultsActivity" />
    </activity>
    <activity
        android:name="com.example.myfirstapp.SearchResultsActivity"
        android:label="@string/title_activity_search_results"
        android:launchMode="singleTop" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>
    <activity
        android:name="com.example.myfirstapp.AddContacts"
        android:label="@string/title_activity_add_contacts" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.myfirstapp.ContactActivity"
        android:label="@string/title_activity_contact" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.myfirstapp.DisplayInfo"
        android:label="@string/title_activity_display_info" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <provider
        android:name="com.example.myfirstapp.SomeProvider"
        android:authorities="com.example.myfirstapp.SomeProvider" >
    </provider>

    <activity
        android:name="com.example.myfirstapp.EditContacts"
        android:label="@string/title_activity_edit_contacts" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.myfirstapp.DeleteContact"
        android:label="@string/title_activity_delete_contact" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.myfirstapp.FlightInfo"
        android:label="@string/title_activity_flight_info" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.myfirstapp.EventInfo"
        android:label="@string/title_activity_event_info" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.myfirstapp.ContactsActivity"
        android:label="@string/title_activity_contacts" >
    </activity>
    <activity
        android:name="com.example.myfirstapp.ShowActivity"
        android:label="@string/title_activity_map" >
    </activity>

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="removed" />
<uses-library android:name="com.google.android.maps" />
</application>

</manifest>

最佳答案

试试下面的代码:

public class ShowActivity extends android.support.v4.app.FragmentActivity {
    private GoogleMap mMap;

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

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }
}

XML文件代码:

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

list 文件代码:

    <permission
        android:name="com.example.myfirstapp.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>
      <uses-permission android:name="com.example.myfirstapp.permission.MAPS_RECEIVE"/>
        <!-- Copied from Google Maps Library/AndroidManifest.xml. -->
      <uses-sdk
                android:minSdkVersion="8"
                android:targetSdkVersion="16"/>
        <uses-permission android:name="android.permission.INTERNET"/>
      <uses-permission         android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- External storage for caching. -->
       <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
      <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>
      <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >    
         <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="removed" />
   </application>

关于android - 二进制 XML 文件行 #8 : Error inflating class fragment, Google map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14825331/

相关文章:

android - MapFragment 和 Camera 似乎在同一个 Activity 中相互干扰

android - 我们可以将 Phonegap 与 Java for Android 一起使用吗

android view flipper,为每张图片干杯

Android:无法在 WebView 中加载具有 javascript 文件的简单 html 数据?

java - SDO API : how to make XSDHelper. 实例是否正确定义带有 <include> 的 XSD 架构?

java - 自定义 KeyboardView 上的 keyOutputText 属性不输出任何文本

android - 制作圆形 FrameLayout

android - MPAndroidChart 如何设置标签颜色?

google-maps-android-api-2 - API 级别 8 至 17 的 Google Maps API V2 Android 应用程序

android - 使用 SupportMapFragment 而不是 MapFragment