首页 文章

Android谷歌 Map :错误膨胀类片段

提问于
浏览
0

我是Android开发人员的新手,有时候我正试图在我的应用程序中放置一个 Map . 我试了一些东西,但没有成功 . 如果有人可以帮助我,我感激不尽 .

我做过的事情:

  • 在我的Package Explorer中添加了google-play-services_lib .

  • 包含android-support-v4.jar和google-play-services.jar作为我项目的依赖项 .

我已经尝试了几个建议,但谷歌 Map API v2 Android不起作用 .

运行我的项目时出现以下错误:

06-15 20:31:37.666: E/AndroidRuntime(15687): FATAL EXCEPTION: main
06-15 20:31:37.666: E/AndroidRuntime(15687): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hellomap/com.example.hellomap.MainActivity}: android.view.InflateException: Binary XML file line #3: Error inflating class fragment
06-15 20:31:37.666: E/AndroidRuntime(15687):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at android.app.ActivityThread.access$700(ActivityThread.java:134)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at android.os.Looper.loop(Looper.java:137)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at android.app.ActivityThread.main(ActivityThread.java:4867)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at java.lang.reflect.Method.invokeNative(Native Method)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at java.lang.reflect.Method.invoke(Method.java:511)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at dalvik.system.NativeStart.main(Native Method)
06-15 20:31:37.666: E/AndroidRuntime(15687): Caused by: android.view.InflateException: Binary XML file line #3: Error inflating class fragment
06-15 20:31:37.666: E/AndroidRuntime(15687):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:316)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at android.app.Activity.setContentView(Activity.java:1901)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at com.example.hellomap.MainActivity.onCreate(MainActivity.java:14)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at android.app.Activity.performCreate(Activity.java:5047)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
06-15 20:31:37.666: E/AndroidRuntime(15687):    ... 11 more
06-15 20:31:37.666: E/AndroidRuntime(15687): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value.  Expected 4323000 but found 0.  You must have the following declaration within the  element:     
06-15 20:31:37.666: E/AndroidRuntime(15687):    at com.google.android.gms.common.GooglePlayServicesUtil.t(Unknown Source)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at com.google.android.gms.maps.internal.q.B(Unknown Source)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at com.google.android.gms.maps.internal.q.A(Unknown Source)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at com.google.android.gms.maps.SupportMapFragment$b.gW(Unknown Source)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at com.google.android.gms.maps.SupportMapFragment$b.a(Unknown Source)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at com.google.android.gms.dynamic.a.a(Unknown Source)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:290)
06-15 20:31:37.666: E/AndroidRuntime(15687):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
06-15 20:31:37.666: E/AndroidRuntime(15687):    ... 20 more

Here is main.xml file `<?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://schemas.android.com/apk/res-auto" android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" map:mapType="normal"/>` Here is manifest file ``
`<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.hellomap" android:versionCode="1" android:versionName="1.0">

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

<!-- The following four permissions -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<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"/>

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

<application
        android:label="@string/app_name"
        android:icon="@drawable/ic_launcher">

    <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyDD0f1pf-64LHNlTUWl6a5qeoWqqMw0e3s"/>

    <activity
     android:name="MainActivity"
     android:label="@string/app_name">
         <intent-filter>
         <action android:name="android.intent.action.MAIN"/>
         <category android:name="android.intent.category.LAUNCHER"/>
         </intent-filter>
    </activity>
</application>

</manifest>`` **and finally java** ``package com.example.hellomap;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}`
`` 坦克的

1 回答

  • 0

    试试我的其他帖子 . 我已经详细解释了 Map 的工作原理 .

    您现在应该可以使用 Map 了 .

    Want to use GoogleMaps - OnMyLocationChangeListener but can't implement it? Any other options

    这是我正在使用的 Map 的布局代码段:

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

    许可是这样的:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.mike.mylocation"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="19" />
    
        <permission
            android:name="com.mike.maps.permission.MAPS_RECEIVE"
            android:protectionLevel="signature" />
    
        <uses-permission android:name="com.example.mapsexample.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" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    
        <uses-feature
            android:glEsVersion="0x00020000"
            android:required="true" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.mike.mylocation.HomeActivity"
                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.gms.version"
                android:value="@integer/google_play_services_version" />
            <meta-data
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="YOUE API WILL GO HERE" />
        </application>
    
    </manifest>
    

    另外不要忘记使用这些 callbacks 或你的电池将被耗尽..:p

    @Override
        protected void onPause() {
    
            super.onPause();
            locationManager.removeUpdates(this);
    
        }
    
    
        @Override
        protected void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
            locationManager.removeUpdates(this);
        }
    
    
        @Override
        protected void onResume() {
            // TODO Auto-generated method stub
            super.onResume();
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                    1, this);
        }
    

    EDIT:

    如果对上一个答案有疑问,请参考以下代码:

    1) In your MainActivity, extend the Fragment Activiy and implement LocationListener, OnMapClickListener, OnMapLongClickListener 喜欢这个:

    public class MainActivity extends FragmentActivity implements LocationListener,
            OnMapClickListener, OnMapLongClickListener {
    
    private static final int GPS_ERRORDIALOG_REQUEST = 9001;
    
        GoogleMap map;
        List<Address> matches;
        String addressText;
        String addressLine;
        double latitude;
        double longitude;
        AppUtils mAppUtils;
        Context context;
        LocationManager locationManager;
    
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            context = this;
    
            if (servicesOK()) {
                // Toast.makeText(this, "Ready to map!!", Toast.LENGTH_LONG).show();
                setContentView(R.layout.activity_main);
    
                // Getting reference to the SupportMapFragment of activity_main.xml
                SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.map);
    
                // Getting GoogleMap object from the fragment
                map = fm.getMap();
    
                // Enabling MyLocation Layer of Google Map
                map.setMyLocationEnabled(true);
    
                // Getting LocationManager object from System Service
                // LOCATION_SERVICE
                locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    
                // Creating a criteria object to retrieve provider
                Criteria criteria = new Criteria();
    
                // Getting the name of the best provider
                String provider = locationManager.getBestProvider(criteria, true);
    
                // Getting Current Location
                Location location = locationManager.getLastKnownLocation(provider);
    
                if (location != null) {
                    onLocationChanged(location);
                }
                locationManager.requestLocationUpdates(provider, 20000, 0, this);
    
            } else {
                setContentView(R.layout.activity_main);
    
            }
    
        }
    
    @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
        }
    
        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
        }
    
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub
        }
    
            //This is the main stuff here. This is when the location is changed.
            @Override
        public void onLocationChanged(Location location) {
    
            // Getting latitude of the current location
            latitude = location.getLatitude();
    
            // Getting longitude of the current location
            longitude = location.getLongitude();
    
            // Creating a LatLng object for the current location
            LatLng latLng = new LatLng(latitude, longitude);
    
            // Showing the current location in Google Map
            map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    
            // Zoom in the Google Map
            map.animateCamera(CameraUpdateFactory.zoomTo(12));
    
            map.addMarker(
                    new MarkerOptions().position(new LatLng(latitude, longitude))
                            .title("My Location" + "\n")).showInfoWindow();
    
            map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
    
                @Override
                public boolean onMarkerClick(Marker marker) {
    
                    marker.showInfoWindow();
    
                    return false;
                }
    
            });
    
    
                //This is optional........................................................
            Geocoder geoCoder = new Geocoder(this);
    
            try {
                matches = geoCoder.getFromLocation(latitude, longitude, 1);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Address bestMatch = (matches.isEmpty() ? null : matches.get(0));
            addressText = String.format("%s, %s, %s", bestMatch
                    .getMaxAddressLineIndex() > 0 ? bestMatch.getAddressLine(0)
                    : "", bestMatch.getLocality(), bestMatch.getCountryName());
              //...........................................................................
    
    
        }
    
        @Override
        public void onMapLongClick(LatLng point) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onMapClick(LatLng point) {
            // TODO Auto-generated method stub
    
        }
    
    //Check if the service is OK
    
    public boolean servicesOK() {
    
            int isAvailable = GooglePlayServicesUtil
                    .isGooglePlayServicesAvailable(this);
    
            if (isAvailable == ConnectionResult.SUCCESS) {
    
                return true;
    
            } else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
    
                Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable,
                        this, GPS_ERRORDIALOG_REQUEST);
                dialog.show();
    
            } else {
    
                Toast.makeText(this, "Cant connect!!", Toast.LENGTH_SHORT).show();
    
            }
            return false;
        }
    
        @Override
        protected void onPause() {
    
            super.onPause();
            locationManager.removeUpdates(this);
    
        }
    
    
        @Override
        protected void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
            locationManager.removeUpdates(this);
        }
    
    
        @Override
        protected void onResume() {
            // TODO Auto-generated method stub
            super.onResume();
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                    1, this);
        }
    

    2) Layout is the same as mentioned above.

    希望这有帮助.. :)

相关问题