【问题标题】:google map - runtimeexception - error inflating class fragment谷歌地图 - 运行时异常 - 错误膨胀类片段
【发布时间】:2014-02-13 01:58:28
【问题描述】:

我只是尝试在显示屏上查看地图,但它不起作用。我得到一个运行时异常:

01-17 19:16:47.066: E/AndroidRuntime(6605): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.maptest/com.example.maptest.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment

Java:

import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.MapFragment;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;




public class MainActivity extends FragmentActivity {
 getSupportFragmentManager().findFragmentById(R.id.map))

@Override
protected void onCreate(Bundle savedInstanceState) {
    GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    SupportMapFragment fragment = new SupportMapFragment();
    getSupportFragmentManager().beginTransaction()
            .add(android.R.id.content, fragment).commit();

    setContentView(R.layout.activity_main);

    GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

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


}

清单

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

<uses-sdk
    android:minSdkVersion="8"
            android:targetSdkVersion="16"/>
  <permission
      android:name="com.example.maptest.permission.MAPS_RECEIVE"
      android:protectionLevel="signature"/>
    <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="com.example.mapdemo.permission.MAPS_RECEIVE"/>


    <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" >
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="removed"/>


    <activity
        android:name="com.example.maptest.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <category android:name="android.intent.category.EMBED"/>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>
           <uses-library android:name="com.google.android.maps"/>
</application>

</manifest>

布局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=".MainActivity" >

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

关键是 v2,我严格按照我找到的 google 教程进行操作。我还导入了安卓播放服务。

有人可以帮帮我吗? :)

【问题讨论】:

  • 在你的 oncreate 方法 setContentView(R.layout.activity_main);调用了两次

标签: android google-maps google-maps-android-api-2


【解决方案1】:

我在尝试 Map API v2 时遇到了类似的错误。不过不知道对你有没有帮助。

我的问题是 google-play-services_lib 必须作为库包含在内。在 Eclipse 中,您可以这样做:

  1. 使用 Android SDK 管理器下载库。
  2. 在您用于安装 SDK 的文件夹中指向 google-play-services_lib 的位置创建一个新的“Android Project from existing code”。
  3. 在“项目 -> 属性 -> Android”下将此项目标记为“是库”。
  4. 在您的项目中,同样打开“项目 -> 属性 -> Android”并单击库部分中的“添加”。选择 google-play-services_lib。

至少这对我有用。

【讨论】:

  • 嗯,我明白你的意思。但是我有一个问题:当我尝试从现有代码创建一个项目并导航到该项目时,该项目显示为灰色字母,所以我无法检查它...你知道为什么会这样吗?
  • 我不确定你的意思。你能提供一张图片吗?但是,我不是 Eclipse 专家,并且在这个问题上苦苦挣扎了几个小时......
  • 对不起,我无法插入图片,因为我是这里的新用户。当我尝试将库项目导入我的工作区时,它告诉我,它已经在那里,但我认为它不是
猜你喜欢
  • 2012-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-02
  • 1970-01-01
  • 1970-01-01
  • 2014-07-26
相关资源
最近更新 更多