Inflating Google Maps v2 fragment causes ClassNotF

2019-07-31 10:36发布

问题:

Sorry i am posting this question on. I have refered so many links. But still not able to solve the issue

  1. I have linked the google-play-services_lib service.
  2. Set the property to Google Inc.
  3. Get the API_KEY from google console.

This is my Manifest.xml:

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

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

 <permission
    android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

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

<uses-permission android:name="com.example.mapdemo.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" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

     <uses-library android:name="com.google.android.maps"/>

    <activity
        android:name="com.abc.xyz.MapViews"
        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="removed" />

</application>

</manifest>

This is my .java file:

public class MapViews extends Activity {

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

And this is my map_views.xml file:

<?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="wrap_content"
       android:layout_height="match_parent" />

Add I'm getting the error:

java.lang.RuntimeException: Unable to start activity \
    ComponentInfo{com.abc.xyz/com.abc.xyz.MapViews}: \
    android.view.InflateException: Binary XML file line #2: \
    Error inflating class fragment
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
 at android.app.ActivityThread.access$2300(ActivityThread.java:125)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
 at android.os.Handler.dispatchMessage(Handler.java:99)
 at android.os.Looper.loop(Looper.java:123)
 at android.app.ActivityThread.main(ActivityThread.java:4627)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:521)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
 at dalvik.system.NativeStart.main(Native Method)

Caused by: android.view.InflateException: Binary XML file line #2: \
    Error inflating class fragment
 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
 at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
 at android.app.Activity.setContentView(Activity.java:1647)
 at com.abc.xyz.MapViews.onCreate(MapViews.java:12)
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
 ... 11 more

Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader \
    dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:\
    /data/app/com.abc.xyz-2.apk]
 at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
 at android.view.LayoutInflater.createView(LayoutInflater.java:466)
 at android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
 at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
 ... 19 more`

回答1:

If you're using fragments on older devices (pre Honeycomb) you should always extend your Activity from FragmentActivity.

So, solution for your problem: change your MapViews class to extend FragmentActivity.

import android.support.v4.app.FragmentActivity;

public class MapViews extends FragmentActivity {
  ...

More info and a complete example: http://android-er.blogspot.co.uk/2012/12/using-supportmapfragment.html

Explanation:

The exception what is important here is: java.lang.ClassNotFoundException: android.view.fragment.

The reason for this is that on pre-Honeycomb devices when the Activity tries to load the xml layout it finds the keyword fragment which it can't understand so it tries to load it as a simple built-in view class (android.view.fragment).

If you extend FragmentActivity the setContentView method is overridden so it can process the fragment keyword.



回答2:

There's something wrong in your layout. Just try to define MapView like this:

<com.google.android.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:apiKey="Your Maps API Key goes here"
/>