【问题标题】:Android-> android.view.InflateException: Binary XML file line #2: Error inflating class fragmentAndroid-> android.view.InflateException: Binary XML file line #2: Error inflating class fragment
【发布时间】:2015-12-17 16:19:45
【问题描述】:

我已经尝试了类似问题中给出的所有解决方案,但仍然无法消除使用地图时膨胀类片段的错误。事实上,我的代码与类似问题的解决方案之一中给出的代码完全相同,但代码仍然无法运行。

----------AndroidManifest.xml---------- ---------

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

<uses-sdk
    android:minSdkVersion="12"
    android:targetSdkVersion="21" />
<permission 
 android:name="com.lavisor.mapsdemo.permission.MAPS_RECEIVE"
 android:protectionLevel="signature"/>


<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.lavisor.mapsdemo.permission.MAPS_RECEIVE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICE"/>
<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"/>

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

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

</manifest>

-----------activity_main.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.lavisor.mapsdemo.MainActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

</RelativeLayout>

--------------activity_map.xml------ ----------------

 <?xml version="1.0" encoding="utf-8"?>
 <fragment 
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:map="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
    android:id="@+id/map"
    class="com.google.android.gms.maps.SupportMapFragment"
 />

--------MainActivity.xml------------------------- -

public class MainActivity extends FragmentActivity {


private static final int GPS_ERRORCODE_CONST = 9001;
GoogleMap mMap;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if(servicesOK())
    {
        Toast.makeText(this,"Preparing Map... " , Toast.LENGTH_SHORT).show();

        setContentView(R.layout.activity_map);


    }

    else{
    setContentView(R.layout.activity_main);
    }

 }



 @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);
 }

//to check if google play services are available on device

 public boolean servicesOK()
 {
    int isAvailable;

    isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

    if(isAvailable == ConnectionResult.SUCCESS)
    {
        return true;

    }
    else if(GooglePlayServicesUtil.isUserRecoverableError(isAvailable))
    {

        Dialog diag = GooglePlayServicesUtil.getErrorDialog(isAvailable,this, GPS_ERRORCODE_CONST);
        diag.show();
    }

    else
    {
        Toast.makeText(this,"Cannot Connect to Google Play Services " , Toast.LENGTH_SHORT).show();
    }
    return false;

   }
}

【问题讨论】:

标签: android google-maps android-fragmentactivity


【解决方案1】:


您应该尝试使用SupportMapFragment 而不是MapFragment

另外,请确保您的 build.gradle 具有
com.google.android.gms:play-services-maps:8.3.0


编辑
尝试使用

android:name="com.google.android.gms.maps.SupportMapFragment"
而不是
class="com.google.android.gms.maps.SupportMapFragment"

【讨论】:

  • 实际上我使用的是 eclipse,而不是 android studios..我将 MapFragment 更改为 SupportMapFragment.. 仍然无法正常工作..
  • 在 Eclipse 上还可以。您是否已将 Google Play Services 作为 Lib Project 导入? (不仅仅是罐子)这点很重要。
  • 我尝试运行其他 Google mapV2 示例并发生同样的错误。请有人帮我解决。我从 --> androidhive.info/2013/08/android-working-with-google-maps-v2 下载了示例地图项目
【解决方案2】:

我花时间完整阅读了 logcat 错误日志,发现它告诉我将其添加到我的清单文件中 -->>

  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

它就像一个魅力......我正在寻找一天的解决方案......但我所要做的就是完整阅读错误日志......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-22
    • 1970-01-01
    • 2017-09-13
    • 2013-10-07
    • 2015-05-01
    • 2014-06-18
    • 2015-01-13
    相关资源
    最近更新 更多