简体   繁体   中英

Google maps view android.view.InflateException: Binary XML File - error inflating class fragment

This happened overnight after finishing this piece of code which pulls coordinates (Latitude and Longitude) from an API and displaying those coordinates with Markers onto a GoogleMap. It was working fine until this morning when I decided to implement a refresh page code. Even after I removed it just to double check, the error still remains.

I need help to understand what is going on, and how can I solve this issue.

XML

<FrameLayout 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="com.example.muhd.findtaxi.MapsActivity" >

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


</FrameLayout>

MapsActivity:

import android.Manifest;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.MarkerOptions;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    JSONParser jsonParser = new JSONParser();
    private GoogleMap mMap;
    private GoogleMap nMap;
    private static final LatLng Tampines = new LatLng(1.3525,103.9446);
    List<TaxiCoordinates> myCoordinates = new ArrayList<TaxiCoordinates>();
    private ProgressDialog pDialog;
    MapsActivity map;
    private static final String apiURL = "http://datamall2.mytransport.sg/ltaodataservice/TaxiAvailability/";
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        MapFragment mapFragment = (MapFragment) getFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        myCoordinates = new ArrayList<TaxiCoordinates>();
        new GetCoordinates().execute();


    }
    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Tampines, 10));

        // Add a marker in Sydney and move the camera
        // LatLng sydney = new LatLng(-34, 151);
        // mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        // mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }

    class GetCoordinates extends AsyncTask<String, String, JSONArray> {
        private ProgressDialog progressDialog;

        @Override
        protected JSONArray doInBackground(String... params)
        {
            try {
                JSONArray json = jsonParser.makeHttpRequest(
                        apiURL, "GET");

                if (json != null) {
                    Log.d("JSON result", json.toString());

                    return json;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = new ProgressDialog(MapsActivity.this);
            progressDialog.setMessage("Loading Taxis...");
            progressDialog.show();
        }

        @Override
        protected void onPostExecute(JSONArray jsonArray) {

            if (jsonArray != null)
            {
                //loop
                for(int i = 0; i < jsonArray.length(); i++)
                {
                    try
                    {
                        JSONObject c = jsonArray.getJSONObject(i);
                        TaxiCoordinates temp = new TaxiCoordinates();
                        temp.setLatitude(c.getDouble("Latitude"));
                        temp.setLongitude(c.getDouble("Longitude"));
                        myCoordinates.add(temp);
                        if(progressDialog!=null && progressDialog.isShowing())
                        {
                            progressDialog.dismiss();
                        }

                    }
                    catch (JSONException e)
                    {
                        e.printStackTrace();
                    }
                }
                addMarkers();
            }
        }
    }



    public void addMarkers()
    {
        for(TaxiCoordinates c: myCoordinates)
        {
            double tempLat = c.getLatitude();
            double tempLong = c.getLongitude();
            LatLng pos = new LatLng(tempLat, tempLong);
            mMap.addMarker(new MarkerOptions()
                    .position(pos)
                    .title("Available")
                    .snippet("")
                    .icon(BitmapDescriptorFactory.defaultMarker()));

            //MarkerOptions m =  new MarkerOptions().title("Available").position(new LatLng(tempLat, tempLong));
            //googleMap.addMarker(m);
        }
    }
}

Android Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.muhd.findtaxi">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-feature android:glEsVersion="0x00020000" android:required="true"/>

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

    <!--
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality. 
    -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".RegisterActivity" />
        <!--
             The API key for Google Maps-based APIs is defined as a string resource.
             (See the file "res/values/google_maps_api.xml").
             Note that the API key is linked to the encryption key used to sign the APK.
             You need a different API key for each encryption key, including the release key that is used to
             sign the APK for publishing.
             You can define the keys for the debug and release targets in src/debug/ and src/release/. 
        -->
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="AIzaSyCqGKEImOEKNJO3e1WL2Ex7_NCUbKFD4D8" />

        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps" />
        <activity android:name=".HomeActivity"></activity>
    </application>

</manifest>

EDIT: error points to line 54 in my MapsActivity: setContentView(R.layout.activity_maps);

EDIT2: LOGCAT added

02-24 05:23:00.785 14648-14648/com.example.muhd.findtaxi D/ChimeraCfgMgr: Reading stored module config
02-24 05:23:00.867 14648-14648/com.example.muhd.findtaxi D/ChimeraCfgMgr: Loading module com.google.android.gms.maps from APK /data/user/0/com.google.android.gms/app_chimera/chimera-module-root/module-a3e4fba11e705727c59ff3116ef21fa4834b9f56/MapsModule.apk
02-24 05:23:00.868 14648-14648/com.example.muhd.findtaxi D/ChimeraModuleLdr: Loading module APK /data/user/0/com.google.android.gms/app_chimera/chimera-module-root/module-a3e4fba11e705727c59ff3116ef21fa4834b9f56/MapsModule.apk
02-24 05:23:00.875 14648-14648/com.example.muhd.findtaxi D/ChimeraFileApk: Primary ABI of requesting process is x86
02-24 05:23:00.876 14648-14648/com.example.muhd.findtaxi D/ChimeraFileApk: Classloading successful. Optimized code found.
02-24 05:23:00.893 14648-14648/com.example.muhd.findtaxi W/System: ClassLoader referenced unknown path: /data/user/0/com.google.android.gms/app_chimera/chimera-module-root/module-a3e4fba11e705727c59ff3116ef21fa4834b9f56/native-libs/x86
02-24 05:23:01.097 14648-14648/com.example.muhd.findtaxi I/Google Maps Android API: Google Play services client version: 8487000
02-24 05:23:01.124 14648-14648/com.example.muhd.findtaxi I/Google Maps Android API: Google Play services package version: 8489470
02-24 05:23:01.262 14648-14659/com.example.muhd.findtaxi I/art: Background sticky concurrent mark sweep GC freed 5319(424KB) AllocSpace objects, 4(80KB) LOS objects, 12% free, 4MB/5MB, paused 14.765ms total 38.234ms
02-24 05:23:01.427 14648-14659/com.example.muhd.findtaxi I/art: Background sticky concurrent mark sweep GC freed 4766(238KB) AllocSpace objects, 0(0B) LOS objects, 4% free, 5MB/5MB, paused 6.657ms total 31.136ms
02-24 05:23:01.518 14648-14659/com.example.muhd.findtaxi I/art: Background partial concurrent mark sweep GC freed 1924(168KB) AllocSpace objects, 1(56KB) LOS objects, 39% free, 5MB/8MB, paused 5.994ms total 38.896ms
02-24 05:23:01.567 14648-14648/com.example.muhd.findtaxi W/ContextImpl: Failed to ensure /sdcard/Android/data/com.example.muhd.findtaxi/cache: java.lang.SecurityException: Invalid mkdirs path: /storage/self/primary/Android/data/com.example.muhd.findtaxi/cache
02-24 05:23:01.572 14648-14648/com.example.muhd.findtaxi D/AndroidRuntime: Shutting down VM
02-24 05:23:01.574 14648-14648/com.example.muhd.findtaxi E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.example.muhd.findtaxi, PID: 14648
                                                                           java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.muhd.findtaxi/com.example.muhd.findtaxi.MapsActivity}: android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class fragment
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                            Caused by: android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class fragment
                                                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                                                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                                               at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393)
                                                                               at android.app.Activity.setContentView(Activity.java:2166)
                                                                               at com.example.muhd.findtaxi.MapsActivity.onCreate(MapsActivity.java:53)
                                                                               at android.app.Activity.performCreate(Activity.java:6237)
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:148) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                            Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
                                                                               at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
                                                                               at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                                               at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
                                                                               at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798..

Just take 2 steps and problem would be more likely to get solved:

Step 1: Clean your project by clicking Project -> Clean.

Step 2: Rebuild your project by clicking Project -> Build All.

Also make sure that your layout xml files are syntax error free and you don't have any image which has non-acceptable names (such as a "-" between image name).

Also I request you to have a look at problems window and let me know what errors are being shown there.

see this link Error inflating class fragment

If you want Map inside a Fragment.

You should use a MapView

http://developer.android.com/reference/com/google/android/gms/maps/MapView.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM