如何解决错误膨胀类片段

时间:2014-05-25 15:07:49

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

我正在尝试在我的应用程序中使用谷歌地图。为了实现这一点,我已经下载并添加谷歌播放服务作为我的应用程序的库,我也从谷歌控制台获得了地图密钥。

但是我在logcat中遇到了这个错误 -

   android.view.InflateException: Binary XML file line #23: Error inflating class fragment

我的应用清单文件如下 -

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

  <uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="19" />
  <uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission                                       android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />                 
  <application
    android:allowBackup="true"
    android:icon="@drawable/pin_location"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.surajkumar.myapp.MainActivity"
        android:label="@string/app_tag" >
        <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="@string/gmap_apiv2_key" />
  </application>

  </manifest>

布局文件是 -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    android:background="@color/honeydew" 
>
<TextView 
    android:id="@+id/yourlocationtxt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Refresh to get your location"/>
<LinearLayout 
android:id="@+id/mapsection"
android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="1"
    android:orientation="vertical"
    android:padding="4dp">

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

</LinearLayout>
<Button
    android:id="@+id/sendsms"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/sendlocationbtntxt" 
    android:background="@drawable/button_style"/>

</LinearLayout>

活动代码是 -

package com.surajkumar.myapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


 public class MainActivity extends Activity {

private GoogleMap peoplesLocMap;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    peoplesLocMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();    
  }

  @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) {
        return super.onOptionsItemSelected(item);

}


  }

我在第

行收到错误
  setContentView(R.layout.activity_main);

logcat显示以下内容 -

FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #23: Error inflating class fragment
E/AndroidRuntime(1221):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
E/AndroidRuntime(1221):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
E/AndroidRuntime(1221):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
E/AndroidRuntime(1221):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
E/AndroidRuntime(1221):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
E/AndroidRuntime(1221):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
E/AndroidRuntime(1221):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
E/AndroidRuntime(1221):     at android.app.Activity.setContentView(Activity.java:1895)
E/AndroidRuntime(1221):     at com.surajkumar.myapp.MainActivity.showLocMapView(MainActivity.java:146)
E/AndroidRuntime(1221):     at com.surajkumar.myapp.MainActivity.access$0(MainActivity.java:145)
E/AndroidRuntime(1221):     at com.surajkumar.myapp.MainActivity$2.onClick(MainActivity.java:88)
E/AndroidRuntime(1221):     at android.view.View.performClick(View.java:4240)
E/AndroidRuntime(1221):     at android.view.View$PerformClick.run(View.java:17721)
E/AndroidRuntime(1221):     at android.os.Handler.handleCallback(Handler.java:730)
E/AndroidRuntime(1221):     at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(1221):     at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(1221):     at android.app.ActivityThread.main(ActivityThread.java:5103)
E/AndroidRuntime(1221):     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(1221):     at java.lang.reflect.Method.invoke(Method.java:525)
E/AndroidRuntime(1221):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
E/AndroidRuntime(1221):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime(1221):     at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(1221): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.SupportMapFragment cannot be cast to android.app.Fragment
E/AndroidRuntime(1221):     at android.app.Fragment.instantiate(Fragment.java:585)
E/AndroidRuntime(1221):     at android.app.Fragment.instantiate(Fragment.java:560)
E/AndroidRuntime(1221):     at android.app.Activity.onCreateView(Activity.java:4738)
E/AndroidRuntime(1221):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
E/AndroidRuntime(1221):     ... 21 more

任何人都可以帮忙。我在stackoverflow上尝试了许多建议的解决方案,但似乎都没有。请帮我解决这个问题。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

更改此

class="com.google.android.gms.maps.SupportMapFragment"

class="com.google.android.gms.maps.MapFragment"

考虑到这个https://developer.android.com/about/dashboards/index.html?utm_source=ausdroid.net

,您可以在清单中将min sdk更改为12及以上

如果您想要api级别11及以下的地图,请使用SupportMapFragment扩展FragmentActivity

如果超过12,请使用MapFragment并延长Activity

您还缺少元标记

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

你也错过了

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