无法在Android中显示谷歌地图

时间:2015-10-09 10:39:30

标签: java android google-maps

我正在做一个显示地图的示例我已经使用下面的代码但地图没有显示,显示白屏应用程序关闭警报不幸yourapp关闭。我在编码中做了什么错误请指导我解决这个问题

mainactivity.java

package com.example.mapsample;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.gms.maps.MapFragment;
public class MainActivity extends ActionBarActivity {

// Google Map
private GoogleMap googleMap;

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

    try {
        // Loading map
        initilizeMap();

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

}

/**
 * function to load map. If map is not created it will create it for you
 * */
@SuppressLint("NewApi") private void initilizeMap() {
    if (googleMap == null) {
       /* googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                R.id.map)).getMap()*/;
        googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(android.R.id.content)).getMap();

        // check if map is created successfully or not
        if (googleMap == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

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

}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

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

的manifest.xml

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

<!-- Goolge Maps API Key -->
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />
<permission
    android:name="com.example.mapsample.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />
        <uses-permission   android:name="com.example.mapsample.permission.MAPS_RECEIVE" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission   android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<!-- Required OpenGL ES 2.0. for Maps V2 -->
<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=".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>
    <meta-data
 android:name="com.google.android.maps.v2.API_KEY"
 android:value="my_appkey" />
    </application>

日志文件

10-09 15:09:47.669: I/dalvikvm(364): Could not find method    android.view.ViewGroup.onNestedScrollAccepted, referenced from method   android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
10-09 15:09:47.669: W/dalvikvm(364): VFY: unable to resolve virtual method  11746: Landroid/view/ViewGroup;.onNestedScrollAccepted  (Landroid/view/View;Landroid/view/View;I)V
 10-09 15:09:47.669: D/dalvikvm(364): VFY: replacing opcode 0x6f at 0x0000
 10-09 15:09:47.679: D/dalvikvm(364): VFY: dead code 0x0003-0015 in    Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.onNestedScrollAccept     ed (Landroid/view/View;Landroid/view/View;I)V
  10-09 15:09:47.679: I/dalvikvm(364): Could not find method    android.view.ViewGroup.onStopNestedScroll, referenced from method    android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
 10-09 15:09:47.679: W/dalvikvm(364): VFY: unable to resolve virtual method    11752: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
 10-09 15:09:47.679: D/dalvikvm(364): VFY: replacing opcode 0x6f at 0x0000
 10-09 15:09:47.689: D/dalvikvm(364): VFY: dead code 0x0003-0025 in    Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.onStopNestedScroll    (Landroid/view/View;)V
 10-09 15:09:47.689: I/dalvikvm(364): Could not find method    android.view.ViewGroup.onWindowSystemUiVisibilityChanged, referenced from method     android.support.v7.internal.widget.ActionBarOverlayLayout.onWindowSystemUiVisibi     lityChanged
 10-09 15:09:47.689: W/dalvikvm(364): VFY: unable to resolve virtual method          11754: Landroid/view/ViewGroup;.onWindowSystemUiVisibilityChanged (I)V
 10-09 15:09:47.689: D/dalvikvm(364): VFY: replacing opcode 0x6f at 0x0008
  10-09 15:09:47.709: I/dalvikvm(364): Could not find method   android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll,     referenced from method   android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScroll Enabled
  10-09 15:09:47.709: W/dalvikvm(364): VFY: unable to resolve virtual method 9397:    Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
10-09 15:09:47.709: D/dalvikvm(364): VFY: replacing opcode 0x6e at 0x000e
10-09 15:09:47.739: I/dalvikvm(364): Could not find method    android.widget.FrameLayout.jumpDrawablesToCurrentState, referenced from method     android.support.v7.internal.widget.ActionBarContainer.jumpDrawablesToCurrentStat     e
10-09 15:09:47.739: W/dalvikvm(364): VFY: unable to resolve virtual method    12176: Landroid/widget/FrameLayout;.jumpDrawablesToCurrentState ()V
10-09 15:09:47.739: D/dalvikvm(364): VFY: replacing opcode 0x6f at 0x0006
10-09 15:09:47.739: D/dalvikvm(364): VFY: dead code 0x0009-0023 in    Landroid/support/v7/internal/widget/ActionBarContainer;.jumpDrawablesToCurrentSt    ate ()V
10-09 15:09:47.759: W/dalvikvm(364): VFY: unable to find class referenced in    signature (Landroid/view/ActionMode;)
 10-09 15:09:47.849: I/dalvikvm(364): Could not find method     android.view.ViewGroup.onRtlPropertiesChanged, referenced from method    android.support.v7.widget.Toolbar.onRtlPropertiesChanged
 10-09 15:09:47.849: W/dalvikvm(364): VFY: unable to resolve virtual method    11749: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
 10-09 15:09:47.859: D/dalvikvm(364): VFY: replacing opcode 0x6f at 0x0007
 10-09 15:09:47.879: I/dalvikvm(364): Could not find method     android.content.res.TypedArray.getChangingConfigurations, referenced from method     android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
 10-09 15:09:47.879: W/dalvikvm(364): VFY: unable to resolve virtual method    535: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
 10-09 15:09:47.879: D/dalvikvm(364): VFY: replacing opcode 0x6e at 0x0002
 10-09 15:09:47.889: D/dalvikvm(364): VFY: dead code 0x0005-0006 in     Landroid/support/v7/internal/widget/TintTypedArray;.getChangingConfigurations    ()I
  10-09 15:09:47.889: I/dalvikvm(364): Could not find method  android.content.res.TypedArray.getType, referenced from method  android.support.v7.internal.widget.TintTypedArray.getType
  10-09 15:09:47.889: W/dalvikvm(364): VFY: unable to resolve virtual method    557: Landroid/content/res/TypedArray;.getType (I)I
 10-09 15:09:47.899: D/dalvikvm(364): VFY: replacing opcode 0x6e at 0x0002
 10-09 15:09:47.899: D/dalvikvm(364): VFY: dead code 0x0005-0006 in    Landroid/support/v7/internal/widget/TintTypedArray;.getType (I)I
  10-09 15:09:47.969: I/dalvikvm(364): Could not find method   android.view.accessibility.AccessibilityEvent.setSource, referenced from method    android.support.v7.internal.widget.ActionBarContextView.onInitializeAccessibilit      yEvent
 10-09 15:09:47.969: W/dalvikvm(364): VFY: unable to resolve virtual method     11859: Landroid/view/accessibility/AccessibilityEvent;.setSource   (Landroid/view/View;)V
  10-09 15:09:47.969: D/dalvikvm(364): VFY: replacing opcode 0x6e at 0x000e
  10-09 15:09:47.979: I/dalvikvm(364): Could not find method     android.support.v7.internal.widget.AbsActionBarView.onInitializeAccessibilityEve      nt, referenced from method     android.support.v7.internal.widget.ActionBarContextView.onInitializeAccessibilit      yEvent
  10-09 15:09:47.979: W/dalvikvm(364): VFY: unable to resolve virtual method   9095:    Landroid/support/v7/internal/widget/AbsActionBarView;.onInitializeAccessibilityE     vent (Landroid/view/accessibility/AccessibilityEvent;)V
 10-09 15:09:47.979: D/dalvikvm(364): VFY: replacing opcode 0x6f at 0x002d
 10-09 15:09:47.979: D/dalvikvm(364): VFY: dead code 0x0011-002b in    Landroid/support/v7/internal/widget/ActionBarContextView;.onInitializeAccessibil     ityEvent (Landroid/view/accessibility/AccessibilityEvent;)V
 10-09 15:09:47.979: D/dalvikvm(364): VFY: dead code 0x0030-0030 in     Landroid/support/v7/internal/widget/ActionBarContextView;.onInitializeAccessibil      ityEvent (Landroid/view/accessibility/AccessibilityEvent;)V
  10-09 15:09:48.089: I/dalvikvm(364): Could not find method    android.app.AlertDialog$Builder.<init>, referenced from method    com.google.android.gms.common.GooglePlayServicesUtil.a
10-09 15:09:48.089: W/dalvikvm(364): VFY: unable to resolve direct method    91: Landroid/app/AlertDialog$Builder;.<init> (Landroid/content/Context;I)V
  -09 15:09:48.089: D/dalvikvm(364): VFY: replacing opcode 0x70 at 0x0037
 10-09 15:09:48.099: E/dalvikvm(364): Could not find class    'android.app.Notification$Builder', referenced from method    com.google.android.gms.common.GooglePlayServicesUtil.a
 10-09 15:09:48.099: W/dalvikvm(364): VFY: unable to resolve new-instance 41    (Landroid/app/Notification$Builder;) in    Lcom/google/android/gms/common/GooglePlayServicesUtil;
 10-09 15:09:48.099: D/dalvikvm(364): VFY: replacing opcode 0x22 at 0x0029
 10-09 15:09:48.110: D/dalvikvm(364): VFY: dead code 0x002b-0071 in   Lcom/google/android/gms/common/GooglePlayServicesUtil;.a   (ILandroid/content/Context;)V
 10-09 15:09:48.119: D/dalvikvm(364): DexOpt: couldn't find field    Landroid/content/res/Configuration;.smallestScreenWidthDp
 10-09 15:09:48.119: W/dalvikvm(364): VFY: unable to resolve instance field 65
 10-09 15:09:48.119: D/dalvikvm(364): VFY: replacing opcode 0x52 at 0x0012
 10-09 15:09:48.119: D/dalvikvm(364): VFY: dead code 0x0014-0018 in    Lcom/google/android/gms/common/GooglePlayServicesUtil;.b    (Landroid/content/res/Resources;)Z
 10-09 15:09:48.149: I/dalvikvm(364): Could not find method    android.app.Activity.getFragmentManager, referenced from method    com.google.android.gms.common.GooglePlayServicesUtil.showErrorDialogFragment
  10-09 15:09:48.149: W/dalvikvm(364): VFY: unable to resolve virtual method    32: Landroid/app/Activity;.getFragmentManager ()Landroid/app/FragmentManager;
 10-09 15:09:48.149: D/dalvikvm(364): VFY: replacing opcode 0x6e at 0x0023
 10-09 15:09:48.159: D/dalvikvm(364): VFY: dead code 0x0026-0030 in    Lcom/google/android/gms/common/GooglePlayServicesUtil;.showErrorDialogFragment   (ILandroid/app/Activity;Landroid/support/v4/app/Fragment;ILandroid/content/Dialo     gInterface$OnCancelListener;)Z
 10-09 15:09:48.180: D/AndroidRuntime(364): Shutting down VM
 10-09 15:09:48.180: W/dalvikvm(364): threadid=1: thread exiting with    uncaught exception (group=0x40015560)
 10-09 15:09:48.209: E/AndroidRuntime(364): FATAL EXCEPTION: main
 10-09 15:09:48.209: E/AndroidRuntime(364): java.lang.RuntimeException:    Unable to start activity    ComponentInfo{com.example.mapsample/com.example.mapsample.MainActivity}:     android.view.InflateException: Binary XML file line #6: Error inflating class    fragment
 10-09 15:09:48.209: E/AndroidRuntime(364):     at    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
 10-09 15:09:48.209: E/AndroidRuntime(364):     at    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
 10-09 15:09:48.209: E/AndroidRuntime(364):     at  android.app.ActivityThread.access$1500(ActivityThread.java:117)
 10-09 15:09:48.209: E/AndroidRuntime(364):     at   android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
 10-09 15:09:48.209: E/AndroidRuntime(364):     at    android.os.Handler.dispatchMessage(Handler.java:99)
 10-09 15:09:48.209: E/AndroidRuntime(364):     at   android.os.Looper.loop(Looper.java:123)
 10-09 15:09:48.209: E/AndroidRuntime(364):     at   android.app.ActivityThread.main(ActivityThread.java:3683)
 10-09 15:09:48.209: E/AndroidRuntime(364):     at     java.lang.reflect.Method.invokeNative(Native Method)
 10-09 15:09:48.209: E/AndroidRuntime(364):     at    java.lang.reflect.Method.invoke(Method.java:507)
 10-09 15:09:48.209: E/AndroidRuntime(364):     at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
 10-09 15:09:48.209: E/AndroidRuntime(364):     at    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
 10-09 15:09:48.209: E/AndroidRuntime(364):     at    dalvik.system.NativeStart.main(Native Method)
 10-09 15:09:48.209: E/AndroidRuntime(364): Caused by:   android.view.InflateException: Binary XML file line #6: Error inflating class  fragment
 10-09 15:09:48.209: E/AndroidRuntime(364):     at    android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
 10-09 15:09:48.209: E/AndroidRuntime(364):     at      android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
 10-09 15:09:48.209: E/AndroidRuntime(364):     at  android.view.LayoutInflater.inflate(LayoutInflater.java:408)
10-09 15:09:48.209: E/AndroidRuntime(364):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
 10-09 15:09:48.209: E/AndroidRuntime(364):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
10-09 15:09:48.209: E/AndroidRuntime(364):  at   android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarAct     ivityDelegateBase.java:228)
 10-09 15:09:48.209: E/AndroidRuntime(364):     at    android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:1    02)
10-09 15:09:48.209: E/AndroidRuntime(364):  at    com.example.mapsample.MainActivity.onCreate(MainActivity.java:22)
 10-09 15:09:48.209: E/AndroidRuntime(364):     at    android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 10-09 15:09:48.209: E/AndroidRuntime(364):     at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
 10-09 15:09:48.209: E/AndroidRuntime(364):     ... 11 more
 10-09 15:09:48.209: E/AndroidRuntime(364): Caused by:    java.lang.IllegalStateException: The meta-data tag in your app's    AndroidManifest.xml does not have the right value.  Expected 6587000 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" />
 10-09 15:09:48.209: E/AndroidRuntime(364):     at    com.google.android.gms.common.GooglePlayServicesUtil.D(Unknown Source)
 10-09 15:09:48.209: E/AndroidRuntime(364):     at    com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailab      le(Unknown Source)
  10-09 15:09:48.209: E/AndroidRuntime(364):    at     com.google.android.gms.maps.internal.x.T(Unknown Source)
  10-09 15:09:48.209: E/AndroidRuntime(364):    at    com.google.android.gms.maps.internal.x.S(Unknown Source)
  10-09 15:09:48.209: E/AndroidRuntime(364):    at   com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
 10-09 15:09:48.209: E/AndroidRuntime(364):     at com.google.android.gms.maps.SupportMapFragment$b.nO(Unknown Source)
 10-09 15:09:48.209: E/AndroidRuntime(364):     at com.google.android.gms.maps.SupportMapFragment$b.a(Unknown Source)
 10-09 15:09:48.209: E/AndroidRuntime(364):     at com.google.android.gms.dynamic.a.a(Unknown Source)
 10-09 15:09:48.209: E/AndroidRuntime(364):     at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
 10-09 15:09:48.209: E/AndroidRuntime(364):     at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
 10-09 15:09:48.209: E/AndroidRuntime(364):     at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:215     8)
 10-09 15:09:48.209: E/AndroidRuntime(364):     at    android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
  10-09 15:09:48.209: E/AndroidRuntime(364):    at    android.support.v7.app.ActionBarActivity.onCreateView(ActionBarActivity.java:547      )
   10-09 15:09:48.209: E/AndroidRuntime(364):   at    android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
   10-09 15:09:48.209: E/AndroidRuntime(364):   ... 20 more
  10-09 15:09:50.480: I/Process(364): Sen

2 个答案:

答案 0 :(得分:0)

可能会发生这种情况,因为现在地图尚未就绪。尝试致电mapFragment.getMapAsync(OnMapReadyCallback),然后查看onMapReady(GoogleMap googleMap)

中的地图

答案 1 :(得分:0)

首先,您必须在AndroidManifest文件的元素中包含以下声明:

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

您的Activity类必须如下所示:

public class MapActivity extends Activity implements OnMapReadyCallback {

    private GoogleMap googleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SupportMapFragment mapFragment = getSupportFragmentManager().findFragmentById(android.R.id.content);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        this.googleMap = googleMap;
        Toast.makeText(getApplicationContext(), "Map is ready!", Toast.LENGTH_SHORT).show();
    }
}