在googlemaps片段上放置一个工具栏

时间:2015-07-01 01:14:41

标签: android google-maps android-layout

我在尝试向地图项目添加工具栏时遇到问题。我收到了以下错误:

Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class android.support.v7.widget.Toolbar

我不知道这个错误意味着什么,有人可以帮我解决这个问题吗?

这是我的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=".MapsActivity" >

    <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_alignParentTop="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/material_blue_grey_800"
    android:minHeight="?attr/actionBarSize"/>

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

     </RelativeLayout>

这是我的班级

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity {

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.

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

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

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }


    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }


    private void setUpMap() {


        // Enable MyLocation Layer of Google Map
        mMap.setMyLocationEnabled(true);

        // Get LocationManager object from System Service LOCATION_SERVICE
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        // Create a criteria object to retrieve provider
        Criteria criteria = new Criteria();

        // Get the name of the best provider
        String provider = locationManager.getBestProvider(criteria, true);

        // Get Current Location
        Location myLocation = locationManager.getLastKnownLocation(provider);

        // set map type
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        // Get latitude of the current location

        mMap.getUiSettings().setZoomControlsEnabled(true);
        mMap.getUiSettings().setMyLocationButtonEnabled(true);
        mMap.getUiSettings().setCompassEnabled(true);
        mMap.getUiSettings().setRotateGesturesEnabled(true);
        mMap.getUiSettings().setZoomGesturesEnabled(true);


        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if (location != null) {
            double longitude = location.getLongitude();
            double latitude = location.getLatitude();

              LatLng latLng = new LatLng(latitude, longitude);
             mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

            // Zoom in the Google Map
            LatLng myCoordinates = new LatLng(latitude, longitude);
            CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(myCoordinates, 20);
             mMap.animateCamera(yourLocation);
              mMap.animateCamera(CameraUpdateFactory.zoomTo(20));


        }
    }
}

和我的gradle

  dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.android.gms:play-services:7.5.0'
compile 'com.android.support:appcompat-v7:22.0'
compile 'com.android.support:cardview-v7:21.0.+'
compile 'com.android.support:recyclerview-v7:21.0.+'
}

并且logcat是

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.core.firstmap/com.example.core.firstmap.MapsActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class android.support.v7.widget.Toolbar
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
        Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class android.support.v7.widget.Toolbar
        at android.view.LayoutInflater.createView(LayoutInflater.java:633)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
        at android.app.Activity.setContentView(Activity.java:2145)
        at com.example.curtis.firstmap.MapsActivity.onCreate(MapsActivity.java:24)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                    at android.app.ActivityThread.access$800(ActivityThread.java:151)
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                    at android.os.Handler.dispatchMessage(Handler.java:102)
                    at android.os.Looper.loop(Looper.java:135)
                    at android.app.ActivityThread.main(ActivityThread.java:5254)
                    at java.lang.reflect.Method.invoke(Native Method)
                    at java.lang.reflect.Method.invoke(Method.java:372)
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
        Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
        at android.view.LayoutInflater.createView(LayoutInflater.java:607)
                    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
                    at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
                    at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
                    at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
                    at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
                    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
                    at android.app.Activity.setContentView(Activity.java:2145)
                    at com.example.core.firstmap.MapsActivity.onCreate(MapsActivity.java:24)
                    at android.app.Activity.performCreate(Activity.java:5990)
                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                    at
        android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                    at
        android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                    at android.app.ActivityThread.access$800(ActivityThread.java:151)
                    at
        android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                    at android.os.Handler.dispatchMessage(Handler.java:102)
                    at android.os.Looper.loop(Looper.java:135)
                    at android.app.ActivityThread.main(ActivityThread.java:5254)
                    at java.lang.reflect.Method.invoke(Native Method)
                    at java.lang.reflect.Method.invoke(Method.java:372)
                    at   com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
        Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 216
        at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:569)
        at android.view.View.<init>(View.java:3984)
        at android.view.ViewGroup.<init>(ViewGroup.java:497)
        at android.view.ViewGroup.<init>(ViewGroup.java:493)
        at android.support.v7.widget.Toolbar.<init>(Toolbar.java:196)
        at android.support.v7.widget.Toolbar.<init>(Toolbar.java:192)
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
            at android.view.LayoutInflater.createView(LayoutInflater.java:607)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
            at android.app.Activity.setContentView(Activity.java:2145)
            at com.example.curtis.firstmap.MapsActivity.onCreate(MapsActivity.java:24)
           at android.app.Activity.performCreate(Activity.java:5990)
           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387
           at android.app.ActivityThread.access$800(ActivityThread.java:151)
           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
           at android.os.Handler.dispatchMessage(Handler.java:102)
           at android.os.Looper.loop(Looper.java:135)
           at android.app.ActivityThread.main(ActivityThread.java:5254)
           at java.lang.reflect.Method.invoke(Native Method)
           at java.lang.reflect.Method.invoke(Method.java:372)

1 个答案:

答案 0 :(得分:1)

设计库现已上市,因此请务必在SDK Manager中更新Android支持存储库。然后,您可以开始使用具有单个新依赖项的Design库:

compile&#39; com.android.support:design:22.2.0&#39;

请注意,由于Design库依赖于Support v4和AppCompat支持库,因此在添加Design库依赖项时将自动包含这些库。

您必须更改您的gradle支持库&#39; com.android.support:appcompat-v7:22.0&#39;到&#39; com.android.support:design:22.2.0&#39;

Source