不幸的是,应用程序已经停止使用谷歌地图api v2 for android

时间:2014-02-19 05:17:38

标签: android google-maps

我尝试了三天但我的问题没有解决!我正在使用Google API地图v2制作应用,我成功激活了它...

现在,当我运行我的程序时,它显示此错误:“不幸的是,应用程序已停止”

请帮助这是什么样的错误??

这是一个清单文件:

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

    <permission
        android:name="com.example.maps.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

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

    <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"/>
<!-- The following two permissions are not required to use
     Google Maps Android API v2, but are recommended. -->
    <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" >
        <activity
            android:name="com.example.maps.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="AIzaSyCKewNqqmmp2gwcLuefCAXrTRdQjm-6t-U" />
    </application>

</manifest>

这是activity_main.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=".MainActivity">

    <fragment
        android:id="@+id/map"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:clickable="true" />

</RelativeLayout>

这是一个MainActivity:

package com.example.maps;

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{

SupportMapFragment mMap;
private GoogleMap googleMap;

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

    googleMap = (SupportMapFragment) getSupportFragmentManager() //Now, here showing an error "cannot convert from SupportMapFragment to GoogleMap" and can't run this program.
            .findFragmentById(R.id.map).getMap();
  }
}

这是一个日志cat文件:

02-19 05:08:50.393: I/Process(1405): Sending signal. PID: 1405 SIG: 9
02-19 05:15:42.608: E/dalvikvm(1447): Could not find class 'com.google.android.gms.maps.SupportMapFragment', referenced from method com.example.maps.MainActivity.onCreate
02-19 05:15:42.608: E/dalvikvm(1447): Could not find class 'com.google.android.gms.maps.SupportMapFragment', referenced from method com.example.maps.MainActivity.onCreate
02-19 05:15:42.608: W/dalvikvm(1447): VFY: unable to resolve check-cast 728 (Lcom/google/android/gms/maps/SupportMapFragment;) in Lcom/example/maps/MainActivity;
02-19 05:15:42.696: D/dalvikvm(1447): VFY: replacing opcode 0x1f at 0x0012
02-19 05:15:43.308: D/AndroidRuntime(1447): Shutting down VM
02-19 05:15:43.308: W/dalvikvm(1447): threadid=1: thread exiting with uncaught exception (group=0xa4be8648)
02-19 05:15:43.348: E/AndroidRuntime(1447): FATAL EXCEPTION: main
02-19 05:15:43.348: E/AndroidRuntime(1447): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.maps/com.example.maps.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
02-19 05:15:43.348: E/AndroidRuntime(1447):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
02-19 05:15:43.348: E/AndroidRuntime(1447):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
02-19 05:15:43.348: E/AndroidRuntime(1447):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-19 05:15:43.348: E/AndroidRuntime(1447):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
02-19 05:15:43.348: E/AndroidRuntime(1447):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 05:15:43.348: E/AndroidRuntime(1447):     at android.os.Looper.loop(Looper.java:137)
02-19 05:15:43.348: E/AndroidRuntime(1447):     at android.app.ActivityThread.main(ActivityThread.java:5103)
02-19 05:15:43.348: E/AndroidRuntime(1447):     at java.lang.reflect.Method.invokeNative(Native Method)
02-19 05:15:43.348: E/AndroidRuntime(1447):     at java.lang.reflect.Method.invoke(Method.java:525)
02-19 05:15:43.348: E/AndroidRuntime(1447):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-19 05:15:43.348: E/AndroidRuntime(1447):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-19 05:15:43.348: E/AndroidRuntime(1447):     at dalvik.system.NativeStart.main(Native Method)
02-19 05:15:43.348: E/AndroidRuntime(1447): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
02-19 05:15:43.348: E/AndroidRuntime(1447):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
02-19 05:15:43.348: E/AndroidRuntime(1447):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
02-19 05:15:43.348: E/AndroidRuntime(1447):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
02-19 05:15:43.348: E/AndroidRuntime(1447):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
02-19 05:15:43.348: E/AndroidRuntime(1447):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
02-19 05:15:43.348: E/AndroidRuntime(1447):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
02-19 05:15:43.348: E/AndroidRuntime(1447):     at android.app.Activity.setContentView(Activity.java:1895)
02-19 05:15:43.348: E/AndroidRuntime(1447):     at com.example.maps.MainActivity.onCreate(MainActivity.java:17)
02-19 05:15:43.348: E/AndroidRuntime(1447):     at android.app.Activity.performCreate(Activity.java:5133)
02-19 05:15:43.348: E/AndroidRuntime(1447):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-19 05:15:43.348: E/AndroidRuntime(1447):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
02-19 05:15:43.348: E/AndroidRuntime(1447):     ... 11 more
02-19 05:15:43.348: E/AndroidRuntime(1447): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: make sure class name exists, is public, and has an empty constructor that is public
02-19 05:15:43.348: E/AndroidRuntime(1447):     at android.app.Fragment.instantiate(Fragment.java:592)
02-19 05:15:43.348: E/AndroidRuntime(1447):     at android.app.Fragment.instantiate(Fragment.java:560)
02-19 05:15:43.348: E/AndroidRuntime(1447):     at android.app.Activity.onCreateView(Activity.java:4738)
02-19 05:15:43.348: E/AndroidRuntime(1447):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
02-19 05:15:43.348: E/AndroidRuntime(1447):     ... 21 more
02-19 05:15:43.348: E/AndroidRuntime(1447): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.SupportMapFragment" on path: DexPathList[[zip file "/data/app/com.example.maps-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.maps-2, /system/lib]]
02-19 05:15:43.348: E/AndroidRuntime(1447):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
02-19 05:15:43.348: E/AndroidRuntime(1447):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
02-19 05:15:43.348: E/AndroidRuntime(1447):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
02-19 05:15:43.348: E/AndroidRuntime(1447):     at android.app.Fragment.instantiate(Fragment.java:582)
02-19 05:15:43.348: E/AndroidRuntime(1447):     ... 24 more
02-19 05:15:53.760: I/Process(1447): Sending signal. PID: 1447 SIG: 9

4 个答案:

答案 0 :(得分:0)

在您的代码中,您应该在GoogleMap中对您的地图进行充气,除了您正在将其膨胀为Fragment之外,该地图已经变为SupportMapFragment

更改以下行:

 mMap = (SupportMapFragment) getSupportFragmentManager()
    .findFragmentById(R.id.map);

    googleMap = mMap.getMap();

如下:

 googleMap = (SupportMapFragment) getSupportFragmentManager()
    .findFragmentById(R.id.map).getMap();

使用GoogleMap的实例而不是SupportMapFragment。好像您将使用SupportMapFragment一样,它总是会搜索您为布局中的Fragment定义的类。并将搜索getMap()的方法,这是GoogleMap类的方法。

<强>编辑:

确保您的项目中有以下库。

将play-services-libs添加为项目。 enter image description here

在构建路径中添加库。

enter image description here

答案 1 :(得分:0)

如果您想在模拟器中运行Google地图,请参阅我在this上一个问题中回答的分步过程

答案 2 :(得分:0)

xml中的第一次更改更改此行

android:name="com.google.android.gms.maps.SupportMapFragment"

class="com.google.android.gms.maps.SupportMapFragment"
manifest中的

第二次更改将此内容添加到application tag

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

最重要的是,您已将google-play-sevice-lib添加到您的项目中

enter image description here 检查API-Key是否已使用您的计算机的SHA-1生成Google console;您的包名称是

编辑:

如何import google_play_services_lib确保ur project and library present in same worksapce

答案 3 :(得分:0)

在实施Google Play服务api期间,不幸的是,应用已停止错误的确定镜头解决了答案:

主要活动:

import com.google.android.gms.location.LocationListener;

而不是

 import android.location.LocationListener;

在依赖项中使用这些:(构建gradle文件)

    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.android.support:support-v4:23.0.1'
    compile 'com.google.android.gms:play-services:11.0.1'
    compile 'com.android.support:multidex:1.0.0'

Android清单文件:

  <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

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