在Android上打开谷歌地图时出现InflateException

时间:2014-11-05 17:02:51

标签: android google-maps-api-2

我有一个我无法解决的奇怪错误。有趣的是,源代码在早上就像一个魅力。

这是xml文件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:id="@+id/container"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             tools:context="example.com.application.MapActivity"
             tools:ignore="MergeRootFrame">
<fragment
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment"
        android:name="example.com.application.MapActivity"/>

</FrameLayout>

我的清单是正确的,否则它将被打印在logcat输出中。

这是源代码:

public class MapActivity extends FragmentActivity implements GoogleMap.OnMarkerClickListener,
        GoogleMap.OnInfoWindowClickListener, GooglePlayServicesClient.ConnectionCallbacks,
        GooglePlayServicesClient.OnConnectionFailedListener {


    private GoogleMap googleMap;

    private LocationClient locationClient;
    private Location location;

    /*
    * Define a request code to send to Google Play services
    * This code is returned in Activity.onActivityResult
    */
    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;

    // TAG for Logcat
    private static final String TAG = MapActivity.class.getSimpleName();


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


        locationClient = new LocationClient(this, this, this);
        locationClient.connect();

        getActionBar().setHomeButtonEnabled(true);
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (locationClient.isConnected()
                || locationClient.isConnecting()) {
            locationClient.disconnect();
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch(item.getItemId()) {
            case R.id.action_settings:
                startActivity(new Intent(getApplicationContext(), SettingsActivity.class));
                return true;
            case R.id.action_search:
                startActivity(new Intent(getApplicationContext(), SearchActivity.class));
                return true;
            case android.R.id.home:
                Intent homeIntent = new Intent(this, HomeActivity.class);
                homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(homeIntent);
                finish();
        }

        return super.onOptionsItemSelected(item);
    }

    private void initializeMap(List<Store> currentMarkets) {


        if (googleMap == null) {
            googleMap = ((com.google.android.gms.maps.MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

            if (googleMap == null) {
                Crouton.makeText(getParent(), "Sry! Unable to create maps", Style.ALERT).show();
                return;
            }

            googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);


            for (Store market : currentMarkets) {

                MarkerOptions marker = new MarkerOptions().position(new LatLng(market.getLat(), market.getLng()));
                marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher));
                marker.title(market.getName());
                marker.snippet(market.getStreet() + "\n" + market.getZip() + " " + market.getCity());
                googleMap.addMarker(marker);

            }

            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(new LatLng(location.getLatitude(), location.getLongitude()))
                    .zoom(11)
                    .build();

            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));


            googleMap.setMyLocationEnabled(true);

            // UI Settings for Google Map
            googleMap.getUiSettings().setZoomControlsEnabled(false);
            googleMap.getUiSettings().setZoomGesturesEnabled(true);
            googleMap.getUiSettings().setCompassEnabled(true);
            googleMap.getUiSettings().setMyLocationButtonEnabled(true);
            googleMap.getUiSettings().setRotateGesturesEnabled(true);
            googleMap.getUiSettings().setScrollGesturesEnabled(true);
            googleMap.getUiSettings().setTiltGesturesEnabled(true);

            googleMap.setTrafficEnabled(true); 
        }


    }





    @Override
    public void onConnected(Bundle bundle) {
        location = locationClient.getLastLocation();

        List<Store> currentMarkets = Store.listAll(Store.class);

        try {
            // Load the map
            initializeMap(currentMarkets);
        } catch (Exception e) {
            Log.e(TAG, "An Exception occured while loading google maps", e);
        }

    }

    @Override
    public void onDisconnected() {

        Log.i(TAG, "LocationClient is disconnected");

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
/*
* Google Play services can resolve some errors it detects.
* If the error has a resolution, try sending an Intent to
* start a Google Play services activity that can resolve
* error.
*/
        if (connectionResult.hasResolution()) {
            try {
// Start an Activity that tries to resolve the error
                connectionResult.startResolutionForResult(
                        this,
                        CONNECTION_FAILURE_RESOLUTION_REQUEST);
/*
* Thrown if Google Play services canceled the original
* PendingIntent
*/
            } catch (IntentSender.SendIntentException e) {
// Log the error
                Log.e(TAG, "Error occured during resolution", e);
            }
        } else {
/*
* If no resolution is available, display a dialog to the
* user with the error.
*/
            Log.d(TAG, connectionResult.getErrorCode() + " is the result of the onConnectionFailed for the Location Services");
        }
    }
}

这是Logcat输出:

11-05 17:53:23.733    7764-7764/example.com.application E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: example.com.application, PID: 7764
    java.lang.RuntimeException: Unable to start activity ComponentInfo{example.com.application/example.com.application.MapActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2264)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5139)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:343)
            at android.app.Activity.setContentView(Activity.java:1929)
            at example.com.application.MapActivity.onCreate(MapActivity.java:50)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2264)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5139)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f020251
            at android.content.res.Resources.getValue(Resources.java:1179)
            at android.content.res.Resources.getDrawable(Resources.java:728)
            at android.content.res.Resources.getDrawable(Resources.java:710)
            at com.google.maps.api.android.lib6.c.cb.<init>(Unknown Source)
            at com.google.maps.api.android.lib6.c.dw.a(Unknown Source)
            at com.google.maps.api.android.lib6.c.v.a(Unknown Source)
            at com.google.maps.api.android.lib6.c.u.a(Unknown Source)
            at com.google.android.gms.maps.internal.u.onTransact(SourceFile:107)
            at android.os.Binder.transact(Binder.java:361)
            at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
            at com.google.android.gms.maps.MapFragment$a.onCreateView(Unknown Source)
            at com.google.android.gms.dynamic.a$4.b(Unknown Source)
            at com.google.android.gms.dynamic.a.a(Unknown Source)
            at com.google.android.gms.dynamic.a.onCreateView(Unknown Source)
            at com.google.android.gms.maps.MapFragment.onCreateView(Unknown Source)
            at android.app.Fragment.performCreateView(Fragment.java:1700)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:866)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
            at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
            at android.app.Activity.onCreateView(Activity.java:4786)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:343)
            at android.app.Activity.setContentView(Activity.java:1929)
            at example.com.application.MapActivity.onCreate(MapActivity.java:50)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2264)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5139)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
            at dalvik.system.NativeStart.main(Native Method)

我不知道究竟是什么导致了这个错误,所以任何帮助都非常感谢。

提前致谢

2 个答案:

答案 0 :(得分:1)

非常感谢@Blaze Tama的帮助。 我找到了解决我的bug的最简单的解决方案。我刚检查了整个设备的logcat,发现谷歌的gms应用程序崩溃了。由于这个原因,我无法打开地图。我重新启动了设备,一切都像魅力一样。

答案 1 :(得分:0)

从您的logcat来看,错误出现在您的onCreate中,(我猜)代码会夸大XML布局。

尝试删除xmlns:android="http://schemas.android.com/apk/res/android"标记中的fragment

您的片段XML代码应为:

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