简体   繁体   English

错误android.support.v4.app.Fragment $ InstantiationException:无法实例化片段com.google.android.gms.maps.MapFragment

[英]Error android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment

i am following tutorial for using google maps api v2 .i am facing an error error logs are 我正在遵循使用谷歌地图api v2的教程。我面临错误错误日志

05-15 00:08:18.415: E/AndroidRuntime(4547): FATAL EXCEPTION: main
05-15 00:08:18.415: E/AndroidRuntime(4547): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.rtesh.onemore/org.rtesh.onemore.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1830)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.app.ActivityThread.access$1500(ActivityThread.java:132)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.os.Looper.loop(Looper.java:150)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.app.ActivityThread.main(ActivityThread.java:4277)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at java.lang.reflect.Method.invokeNative(Native Method)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at java.lang.reflect.Method.invoke(Method.java:507)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at dalvik.system.NativeStart.main(Native Method)
05-15 00:08:18.415: E/AndroidRuntime(4547): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:250)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.app.Activity.setContentView(Activity.java:1712)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at org.rtesh.onemore.MainActivity.onCreate(MainActivity.java:27)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794)
05-15 00:08:18.415: E/AndroidRuntime(4547):     ... 11 more
05-15 00:08:18.415: E/AndroidRuntime(4547): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.support.v4.app.Fragment.instantiate(Fragment.java:395)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.support.v4.app.Fragment.instantiate(Fragment.java:363)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:264)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
05-15 00:08:18.415: E/AndroidRuntime(4547):     ... 20 more
05-15 00:08:18.415: E/AndroidRuntime(4547): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.MapFragment in loader dalvik.system.PathClassLoader[/data/app/org.rtesh.onemore-1.apk]
05-15 00:08:18.415: E/AndroidRuntime(4547):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.support.v4.app.Fragment.instantiate(Fragment.java:385)
05-15 00:08:18.415: E/AndroidRuntime(4547):     ... 23 more

my MainActivity class is 我的MainActivity类是

@SuppressLint("NewApi")
public class MainActivity extends FragmentActivity {
  static final LatLng HAMBURG = new LatLng(53.558, 9.927);
  static final LatLng KIEL = new LatLng(53.551, 9.993);
  private GoogleMap map;

@SuppressLint("NewApi")
@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
        .getMap();

    if (map!=null){
      Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
          .title("Hamburg"));
      Marker kiel = map.addMarker(new MarkerOptions()
          .position(KIEL)
          .title("Kiel")
          .snippet("Kiel is cool")
          .icon(BitmapDescriptorFactory
              .fromResource(R.drawable.ic_launcher)));
    }

  } 

} }

and activity_main.xml is 和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"
    tools:context=".MainActivity" >

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

</RelativeLayout> 

i gave appropriate permissions in maifest class 我在maifest类中给了适当的权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.rtesh.onemore"
    android:versionCode="1"
    android:versionName="1.0" >
<permission
        android:name="org.rtesh.android.locationapi.maps.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />
<uses-permission android:name="com.org.rtesh.android.locationapi.maps.permission.MAPS_RECEIVE" />
    <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="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="org.rtesh.onemore.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="mykey" />

    </application>

</manifest>

i referenced the google play service library why i am facing this error ??please some one help and please point the major differences between google maps version1 and 2!! 我参考了谷歌播放服务库为什么我面临这个错误?请一些帮助,请指出谷歌地图版本1和2之间的主要差异!

Just try to replace your layout with : 只需尝试用以下内容替换您的布局:

<?xml version="1.0" encoding="utf-8"?>
<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" />

You need to use the SupportMapFragment for API under 11 ! 您需要在11下使用SupportMapFragment for API!

In your MainActivity (or in each activity you want to put the v2 map) you must extend FragmentActivity or Activity either you want to use SupportMapFragment or just MapFragment. 在您的MainActivity中(或者您希望放置v2映射的每个活动中),您必须扩展FragmentActivity或Activity,或者您要使用SupportMapFragment或仅使用MapFragment。 Use MapFragment class only if you are targeting API 12 and above. 仅在定位API 12及更高版本时才使用MapFragment类。 Otherwise, use SupportMapFragment. 否则,请使用SupportMapFragment。 It's a subtle difference but it makes the error. 这是一个微妙的差异,但它会导致错误。

EDIT: 编辑:

Please change your code in FragmentActivity: 请在FragmentActivity中更改您的代码:

map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map))
        .getMap();

Understand the concept. 理解这个概念。 Since you are targeting lower android versions where fragments are not supported, you need to use a support library that takes care of your fragments. 由于您的目标是不支持片段的较低Android版本,因此您需要使用支持片段来处理片段。 So wherever you are using fragments make sure you are importing a fragment from support library. 因此,只要您使用片段,请确保从support库导入片段。

First, like the other responders to this post you should change the minimum Android SDK level to at least 11 首先,与此帖的其他响应者一样,您应该将最低Android SDK级别更改为至少11

android:minSdkVersion="11"

Here is a working example that I made yesterday. 这是我昨天做的一个工作实例。

activity_main.xml activity_main.xml中

<?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" >

<fragment
android:name="com.google.android.gms.maps.SupportMapFragment"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraTargetLat="33.41662287712097"
map:cameraTargetLng="35.21843892856462"
map:cameraZoom="4"/>

</LinearLayout>In MainActivity.java onCreate()

In MainActivity.java onCreate() 在MainActivity.java onCreate()中

public class MainActivity extends FragmentActivity  {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {

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

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

mMap.addMarker(new MarkerOptions()
.position(new LatLng(35.21843892856462, 33.41662287712097))
.title("My Office"));

}

To see my screenshot of the result of the code see my answer in this post. 要查看代码结果的截图,请参阅本文中的答案。 gl hf! gl hf! Google Map V2 not working for me . 谷歌地图V2不适合我。 Any sample? 有样品吗?

public class SearchOffer extends FragmentActivity { 公共类SearchOffer扩展FragmentActivity {

GoogleMap gmpSearchOfferMap;
Context context;

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

    try {
        setContentView(R.layout.search_offers);

        if (gmpSearchOfferMap == null) {
            gmpSearchOfferMap = ((SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.gmpOfferMap)).getMap();
        }

        if (gmpSearchOfferMap != null) {
            gmpSearchOfferMap.setMyLocationEnabled(true);
            gmpSearchOfferMap.getUiSettings().setMyLocationButtonEnabled(
                    true);
            gmpSearchOfferMap.getUiSettings().setZoomControlsEnabled(true);
            gmpSearchOfferMap.getUiSettings().setZoomGesturesEnabled(true);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

} }

Your min sdk is 8. You will need to add support library for api versions below 12 你的min sdk是8.你需要为12以下的api版本添加支持库

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

So for backward compatibality you need add support library 因此,为了向后兼容,您需要添加支持库

android:name="com.google.android.gms.maps.SupportMapFragment "

Since you din't add support library you got classcastexception 既然你没有添加支持库,你就得到了classcastexception

To add support library check the link below 要添加支持库,请查看以下链接

http://developer.android.com/tools/extras/support-library.html http://developer.android.com/tools/extras/support-library.html

For downloading the support fragment 用于下载支持片段

http://developer.android.com/tools/extras/support-library.html#Downloading http://developer.android.com/tools/extras/support-library.html#Downloading

For more information check the link 有关更多信息,请查看链接

https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapFragment https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapFragment

Check the line just above the heading developers guide. 检查标题开发人员指南上方的行。

For more information on map api v2 check the documentation 有关map api v2的更多信息,请查看文档

https://developers.google.com/maps/documentation/android/ https://developers.google.com/maps/documentation/android/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 com.google.android.gms.maps.mapfragment和android.support.v4.app.fragment之间的区别 - difference between com.google.android.gms.maps.mapfragment and android.support.v4.app.fragment 在应用程序中使用地图时出错-com.google.android.gms.maps.MapFragment无法转换为android.support.v4.app.Fragment - Error using Map in app - com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment 错误:MapFragment无法将&#39;android.support.v4.app.Fragment&#39;强制转换为&#39;com.google.android.gms.maps.SupportMapFragment&#39; - Error: MapFragment Cannot cast 'android.support.v4.app.Fragment' to 'com.google.android.gms.maps.SupportMapFragment' 无法在活动中实例化com.google.android.gms.maps.MapFragment片段 - Unable to instantiate fragment com.google.android.gms.maps.MapFragment in Activity 不可转换的类型; 无法将“ androidx.fragment.app.Fragment”转换为“ com.google.android.gms.maps.MapFragment” - Inconvertible types; cannot cast 'androidx.fragment.app.Fragment' to 'com.google.android.gms.maps.MapFragment' 无法将类型“ Android.Support.V4.App.Fragment”转换为“ Android.Gms.Maps.MapFragment” - Cannot convert type 'Android.Support.V4.App.Fragment' to 'Android.Gms.Maps.MapFragment' 原因:android.support.v4.app.Fragment $ InstantiationException:无法实例化片段 - Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment 无法将“ android.support.v4.app.Fragment”转换为“ com.google.android.gms.maps.SupportMapFragment” - cannot cast 'android.support.v4.app.Fragment' to 'com.google.android.gms.maps.SupportMapFragment' android.support.v4.app.Fragment $ InstantiationException - android.support.v4.app.Fragment$InstantiationException 错误:无法将MapFragment强制转换为android.support.v4.app.Fragment - Error: MapFragment cannot be cast to android.support.v4.app.Fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM