错误android.support.v4.app.Fragment $ InstantiationE

2019-10-18 03:29发布

我下面的教程使用谷歌地图API第2版。我现在所面临的错误的错误日志

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

我的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)));
    }

  } 

}

和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> 

我给了适当的权限在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>

我引用了谷歌播放服务库,为什么我面对这个错误??请一些一个帮助,请点的主要差异之间的谷歌地图VERSION1和2!

Answer 1:

刚刚尝试,以取代您的布局:

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

您需要使用SupportMapFragment的API下11!

在您的MainActivity(或每个活动要放在V2地图),您必须扩展FragmentActivity或活动或者你要使用SupportMapFragment或只是MapFragment。 只有当你的目标API 12及以上版本使用MapFragment类。 否则,使用SupportMapFragment。 这是一个微妙的差异,但它使错误。

编辑:

请改变你的代码FragmentActivity:

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

理解这个概念。 既然你的目标,其中的片段,不支持较低的Android版本,你需要使用一个支持库,需要你的碎片的照顾。 因此,无论您使用的片段确保您从导入片段support库。



Answer 2:

首先,像其他应答这个职位,你应该最小的Android SDK级别更改为至少11

android:minSdkVersion="11"

下面是我昨天工作的例子。

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()

在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"));

}

看到我的代码的结果的截图中看到我的答案在这个岗位。 GL HF! 谷歌地图V2不是为我工作。 任何样品?



Answer 3:

公共类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();
    }

}

}



Answer 4:

你分钟SDK是8.您将需要添加支持库低于12 API版本

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

因此,对于落后compatibality你需要添加支持库

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

既然你din't添加支持库,你有ClassCastException异常

要添加支持库检查下面的链接

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

对于下载支持片段

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

欲了解更多信息,请查看链接

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

检查正上方标题开发商引导线。

有关地图API第2版的详细信息查看文档

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



文章来源: Error android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment