Android:谷歌地图错误膨胀类片段

时间:2014-05-19 08:26:16

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

我在应用程序中集成谷歌地图时遇到问题。每当我调试时,它都会显示与膨胀类片段相关的错误。我已尝试过stackoverflow和其他互联网资源的所有解决方案,但它们都没有用,我很沮丧。这是我的Activity类的相关部分。

public class Dashboard extends ActionBarActivity {

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

这是xml。

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/the_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"
map:cameraTilt="45"
map:cameraZoom="14"
/>

这是清单文件

<?xml version="1.0" encoding="utf-8"?>

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

<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="com.test.golfscorecard.MainActivity"
        android:label="@string/app_name" >

    </activity>
    <activity
        android:name="com.test.golfscorecard.Dashboard"
        android:label="@string/title_activity_dashboard" >
         <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="/I have valid api here/" />

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

</application>

logcat的:

05-19 14:00:29.553: E/AndroidRuntime(13285): FATAL EXCEPTION: main
05-19 14:00:29.553: E/AndroidRuntime(13285): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.view9.golfscorecard/com.view9.golfscorecard.Dashboard}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
05-19 14:00:29.553: E/AndroidRuntime(13285):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
05-19 14:00:29.553: E/AndroidRuntime(13285):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
05-19 14:00:29.553: E/AndroidRuntime(13285):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-19 14:00:29.553: E/AndroidRuntime(13285):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
05-19 14:00:29.553: E/AndroidRuntime(13285):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-19 14:00:29.553: E/AndroidRuntime(13285):    at android.os.Looper.loop(Looper.java:137)
05-19 14:00:29.553: E/AndroidRuntime(13285):    at android.app.ActivityThread.main(ActivityThread.java:5103)
05-19 14:00:29.553: E/AndroidRuntime(13285):    at java.lang.reflect.Method.invokeNative(Native Method)
05-19 14:00:29.553: E/AndroidRuntime(13285):    at java.lang.reflect.Method.invoke(Method.java:525)
05-19 14:00:29.553: E/AndroidRuntime(13285):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-19 14:00:29.553: E/AndroidRuntime(13285):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-19 14:00:29.553: E/AndroidRuntime(13285):    at dalvik.system.NativeStart.main(Native Method)
05-19 14:00:29.553: E/AndroidRuntime(13285): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
05-19 14:00:29.553: E/AndroidRuntime(13285):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
05-19 14:00:29.553: E/AndroidRuntime(13285):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
05-19 14:00:29.553: E/AndroidRuntime(13285):    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
05-19 14:00:29.553: E/AndroidRuntime(13285):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
05-19 14:00:29.553: E/AndroidRuntime(13285):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
05-19 14:00:29.553: E/AndroidRuntime(13285):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
05-19 14:00:29.553: E/AndroidRuntime(13285):    at android.app.Activity.setContentView(Activity.java:1895)
05-19 14:00:29.553: E/AndroidRuntime(13285):    at com.view9.golfscorecard.Dashboard.onCreate(Dashboard.java:17)
05-19 14:00:29.553: E/AndroidRuntime(13285):    at android.app.Activity.performCreate(Activity.java:5133)
05-19 14:00:29.553: E/AndroidRuntime(13285):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-19 14:00:29.553: E/AndroidRuntime(13285):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
05-19 14:00:29.553: E/AndroidRuntime(13285):    ... 11 more
05-19 14:00:29.553: E/AndroidRuntime(13285): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
05-19 14:00:29.553: E/AndroidRuntime(13285):    at android.support.v4.app.Fragment.instantiate(Fragment.java:402)
05-19 14:00:29.553: E/AndroidRuntime(13285):    at android.support.v4.app.Fragment.instantiate(Fragment.java:377)
05-19 14:00:29.553: E/AndroidRuntime(13285):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:277)
05-19 14:00:29.553: E/AndroidRuntime(13285):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
05-19 14:00:29.553: E/AndroidRuntime(13285):    ... 21 more

任何帮助都将不胜感激。

3 个答案:

答案 0 :(得分:0)

Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment

由于您使用android.support.v4.app.Fragment使用SupportMapFragment

或作为android:minSdkVersion="12"您可以直接使用android.app.Fragment,在这种情况下,您可以使用当前的xml。

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/the_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
map:cameraTilt="45"
map:cameraZoom="14" />

答案 1 :(得分:0)

更改此

public class Dashboard extends ActionBarActivity {

public class Dashboard extends Activity {

ActionBar本身可从api级别11开始提供。因此不需要ActionbarActivity

使用MapFragment是正确的。

答案 2 :(得分:0)

嗨请更改并检查以下代码。

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

public class MapScreen extends FragmentActivity {

       private GoogleMap mMap;  

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.mapview);
      mMap = ((SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.mapview)).getMap();
      mMap.setMyLocationEnabled(true);
     }
}

您需要拥有googleplayservice库项目。你的项目版本应该支持google api&#39。