本文介绍了java.lang.ClassCastException:Android版谷歌地图V2所致com.google.android.gms.maps.SupportMapFragment不能转换为android.app.Fragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有这个在我的课

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.coffeeandcookies.ushuaiamovil.R;

public class LayMapa2 extends FragmentActivity
{
    @Override
    protected void onCreate(Bundle arg0)
    {
        setContentView(R.layout.lay_mapa2);
        super.onCreate(arg0);
    }
}

这在我的XML

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

这在我的manifiest

<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

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

    <permission
        android:name="com.coffeeandcookies.usuahiamovil.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="com.coffeeandcookies.usuahiamovil.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />

 <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="saasadsad" />

我有谷歌,播放services_lib在我的工作区图书馆proyect和Android的支持 - v4.jar

这是我的logcat

07-17 11:46:10.945: E/AndroidRuntime(8241): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.coffeeandcookies.ushuaiamovil/com.coffeeandcookies.vistas.LayMapa2}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.os.Looper.loop(Looper.java:137)
07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.app.ActivityThread.main(ActivityThread.java:4895)
07-17 11:46:10.945: E/AndroidRuntime(8241):     at java.lang.reflect.Method.invokeNative(Native Method)
07-17 11:46:10.945: E/AndroidRuntime(8241):     at java.lang.reflect.Method.invoke(Method.java:511)
07-17 11:46:10.945: E/AndroidRuntime(8241):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
07-17 11:46:10.945: E/AndroidRuntime(8241):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
07-17 11:46:10.945: E/AndroidRuntime(8241):     at dalvik.system.NativeStart.main(Native Method)
07-17 11:46:10.945: E/AndroidRuntime(8241): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-17 11:46:10.945: E/AndroidRuntime(8241):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:306)
07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.app.Activity.setContentView(Activity.java:1912)
07-17 11:46:10.945: E/AndroidRuntime(8241):     at com.coffeeandcookies.vistas.LayMapa2.onCreate(LayMapa2.java:14)
07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.app.Activity.performCreate(Activity.java:5163)
07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
07-17 11:46:10.945: E/AndroidRuntime(8241):     ... 11 more
07-17 11:46:10.945: E/AndroidRuntime(8241): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.SupportMapFragment cannot be cast to android.app.Fragment
07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.app.Fragment.instantiate(Fragment.java:577)
07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.app.Fragment.instantiate(Fragment.java:552)
07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.app.Activity.onCreateView(Activity.java:4806)
07-17 11:46:10.945: E/AndroidRuntime(8241):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
07-17 11:46:10.945: E/AndroidRuntime(8241):     ... 20 more

谢谢!

推荐答案

切换super.onCreate第一,然后的setContentView。

Switch super.onCreate first, then setContentView.

这是因为super.onCreate调用框架的Activity.onCreate分配膨胀任何布局所需要的资源。因此,必须充气任何布局,XML /编程之前被调用。

This is because super.onCreate calls the framework's Activity.onCreate to allocate the resources needed to inflate any layouts. Hence , must be called before inflating any layouts, xml/programmatic.

这篇关于java.lang.ClassCastException:Android版谷歌地图V2所致com.google.android.gms.maps.SupportMapFragment不能转换为android.app.Fragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 18:05