谷歌地图视图android.view.InflateException:二进制XML文件 - 错误膨胀类片段

时间:2016-02-24 05:00:40

标签: java android xml inflate-exception

这是在完成这段代码后发生的,这段代码从API中提取坐标(纬度和经度),并将这些坐标与Markers一起显示在GoogleMap上。它工作正常,直到今天早上我决定实现刷新页面代码。即使我删除它只是为了仔细检查,错误仍然存​​在。

我需要帮助才能了解正在发生的事情,以及如何解决这个问题。

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>

编辑:错误指向我的MapsActivity中的第54行:setContentView(R.layout.activity_maps);

EDIT2:添加了LOGCAT

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

2 个答案:

答案 0 :(得分:0)

只需采取两个步骤,问题就更有可能得到解决:

第1步:单击项目 - &gt;清理项目;干净。

第2步:点击Project - &gt;重建项目。全部构建。

另外,请确保您的布局xml文件没有语法错误,并且您没有任何具有不可接受名称的图像(例如&#34; - &#34;图像名称之间)。< / p>

另外,我请求您查看问题窗口,让我知道那里显示的错误。

看到这个链接 Error inflating class fragment

答案 1 :(得分:0)

如果你想要一个片段里面的地图。

您应该使用MapView

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