2014-12-06 32 views
0

我是新來的機器人。 我正在使用谷歌地圖的應用程序。但經過多次測試,我遇到了同樣的問題:應用意外停止。我的地圖應用意外停止

我介紹了代碼。

的清單:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.map_project" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="10" 
     android:targetSdkVersion="21" /> 

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

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

    <uses-permission android:name="com.map_project.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" /> 

    <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="MyKeyxxx" /> 
    </application> 

</manifest> 

的map_activity.xml:

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

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

</RelativeLayout> 

而且在MainActivity:

package com.map_project; 

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.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 

public class MainActivity extends FragmentActivity { 


    public void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.map_activity); 

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

     LatLng sydney = new LatLng(-33.867, 151.206); 

     map.setMyLocationEnabled(true); 
     map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13)); 

    } 


} 

,當我跑我的手機上的應用程序,我得到的消息:不幸的是,MyApp已經停止。 如果有人能幫助我,那會很好,因爲我開始絕望。

+0

發佈logcat的 – 2014-12-06 05:25:08

+0

開始時,您的地圖將是無效的。 – Piyush 2014-12-06 05:43:51

回答

0

首先,我想知道你是在模擬器還是Android上測試它 Mobile?

如果您在Emulator確保您創建一個谷歌API基於模擬器此模擬器也可用於測試Google地圖和其他Google Play服務集成。

1)其確定如果你使用FragmentManager代替SupportFragmentManager 並導入一樣!

2)嘗試宣告static final LatLng sydney = new LatLng(-33,151);onCreate方法之前

3)Instead of Extending FragmentActivity you can extend Activity 

您的代碼看起來不錯! 但您可以重新檢查以下有關API密鑰的步驟:

4)確保您已在GoogleAPI控制檯中正確創建了API密鑰,並且還激活了Google Maps Android API。 (谷歌控制檯 - >服務 - >它們)

使用的try-catch它會幫助你跟蹤你的錯誤

try { 
     if (googleMap == null) 
     { 
      googleMap = ((MapFragment) getFragmentManager(). 
      findFragmentById(R.id.map)).getMap(); 
     } 
     googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
     Marker syd = googleMap.addMarker(new MarkerOptions(). 
     position(sydney).title("Its Sydney")); 
    } catch (Exception e) 
     { 
     e.printStackTrace(); 
     } 
+0

謝謝你的回覆。我嘗試了修正案。不幸的是問題仍在繼續一開始我有這樣的信息:找不到google-play-services_lib.apk!但即使設置後,應用程序仍繼續停止 – 2014-12-07 23:19:03

+0

我認爲那麼問題是系統無法找到class =「com.google.android.gms.maps.SupportMapFragment」,所以請嘗試這樣做:轉到Project - >屬性 - > android,看看你的項目是在你的項目中包含Android maps.jar,然後進入項目 - >屬性 - > java構建路徑 - >庫 - >谷歌apis,然後選擇一個apis並按照下面的步驟進行確保你在清單文件中提到它是... 2014-12-08 03:21:24