谷歌地图开始崩溃

时间:2014-01-20 16:53:13

标签: android google-maps android-fragments

我尝试从各种代码将谷歌地图添加到我的应用程序,但似乎总是出错。经过一整天的尝试修复和其他代码我迷失了,请帮助我找出为什么当我点击进入我的地图页面我的应用程序崩溃。我已将其设置为使用Google Play服务并获得了我的api密钥。我是android的新手,所以任何帮助都会很棒。

log cat:

01-20 11:44:08.417: E/AndroidRuntime(5118): FATAL EXCEPTION: main
01-20 11:44:08.417: E/AndroidRuntime(5118): Process: com.databasedemo, PID: 5118
01-20 11:44:08.417: E/AndroidRuntime(5118): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.databasedemo/com.databasedemo.Map}: android.view.InflateException: Binary XML file line #5: Error inflating class fragment
01-20 11:44:08.417: E/AndroidRuntime(5118):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at android.os.Looper.loop(Looper.java:136)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at android.app.ActivityThread.main(ActivityThread.java:5017)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at java.lang.reflect.Method.invokeNative(Native Method)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at java.lang.reflect.Method.invoke(Method.java:515)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at dalvik.system.NativeStart.main(Native Method)
01-20 11:44:08.417: E/AndroidRuntime(5118): Caused by: android.view.InflateException: Binary XML file line #5: Error inflating class fragment
01-20 11:44:08.417: E/AndroidRuntime(5118):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at android.app.Activity.setContentView(Activity.java:1929)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at com.databasedemo.Map.onCreate(Map.java:27)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at android.app.Activity.performCreate(Activity.java:5231)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
01-20 11:44:08.417: E/AndroidRuntime(5118):     ... 11 more
01-20 11:44:08.417: E/AndroidRuntime(5118): Caused by: java.lang.IllegalStateException: The     meta-data tag in your app's AndroidManifest.xml does not have the right value.  Expected 4030500 but found 0.  You must have the following declaration within the <application> element:     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
01-20 11:44:08.417: E/AndroidRuntime(5118):     at com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at com.google.android.gms.maps.internal.q.v(Unknown Source)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at com.google.android.gms.maps.internal.q.u(Unknown Source)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at com.google.android.gms.maps.MapFragment$b.cE(Unknown Source)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at com.google.android.gms.maps.MapFragment$b.a(Unknown Source)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at com.google.android.gms.dynamic.a.a(Unknown Source)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at android.app.Activity.onCreateView(Activity.java:4785)
01-20 11:44:08.417: E/AndroidRuntime(5118):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
01-20 11:44:08.417: E/AndroidRuntime(5118):     ... 21 more

我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.databasedemo"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="12"
    android:targetSdkVersion="18" />

<permission android:name="com.image.app.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.image.app.permission.MAPS_RECEIVE" />

<!-- Access Internet -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- Take picture -->
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- GET Latitude Longitude -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<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:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <meta-data android:name="com.google.android.maps.v2.API_KEY"
    android:value="myapikey was here" />

    <activity
        android:name="com.databasedemo.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>

    <!--  EVENTS PAGE -->
    <activity
        android:name="com.databasedemo.Events"
        android:label="@string/app_name" 
        >
         <intent-filter>
            <action android:name="my.activity.events" />

            <category android:name="android.intent.category.Default" />
        </intent-filter>
    </activity>

    <!--  MAPS PAGE -->
    <activity
        android:name="com.databasedemo.Map"
        android:label="@string/app_name" 
        >
         <intent-filter>
            <action android:name="my.activity.map" />

            <category android:name="android.intent.category.Default" />
        </intent-filter>
    </activity>

</application>

</manifest>

我的map.java文件

package com.databasedemo;

import com.databasedemo.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class Map extends FragmentActivity implements OnInfoWindowClickListener 
{
   private LatLng defaultLatLng = new LatLng(39.233956, -77.484703);
   private GoogleMap map;
   private int zoomLevel = 7;

  protected void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_layout);


    try {
        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                    .getMap();
        if (map!=null){
           map.getUiSettings().setCompassEnabled(true);
           map.setTrafficEnabled(true);
           map.setMyLocationEnabled(true);


           // Move the camera instantly to defaultLatLng.
           map.moveCamera(CameraUpdateFactory.newLatLngZoom(defaultLatLng, zoomLevel));


           map.addMarker(new MarkerOptions().position(defaultLatLng)
                .title("This is the title")
                .snippet("This is the snippet within the InfoWindow")
                .icon(BitmapDescriptorFactory
                        .fromResource(R.drawable.icon)));


           map.setOnInfoWindowClickListener(this);


         }


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


  }


   @Override
   public void onPause() {
                if (map != null){
                                map.setMyLocationEnabled(false);
                                map.setTrafficEnabled(false);
                }
                super.onPause();
   }



   @Override
   public void onInfoWindowClick(Marker marker) 
   {
                Intent intent = new Intent(this, Map.class);
                intent.putExtra("snippet", marker.getSnippet());
                intent.putExtra("title", marker.getTitle());
                intent.putExtra("position", marker.getPosition());
                startActivity(intent);
   }


}

maplayout.xml文件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"      
android:layout_width="match_parent" android:layout_height="match_parent"     android:id="@+id/frame">

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

1 个答案:

答案 0 :(得分:0)

尝试将Google Play服务版本元标记添加到AndroidManifest.xml:

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