10

嗨,每一個我知道谷歌地圖已棄用其以前的版本API v1,並推出了新版本的谷歌地圖API v2。我嘗試了一個例子,通過在谷歌中的一些鏈接,我敢肯定,我通過提供確切的散列碼代碼並設法得到正確的API密鑰,正確地得到了API密鑰。現在我設法寫一些代碼,但當我試圖執行代碼我得到的錯誤,請幫我解決這裏這裏是我的代碼如何實施谷歌地圖新版本的API v2

我甚至嘗試了谷歌播放服務提供的示例代碼我得到了同樣的問題

這是我參照在谷歌

主要活動課

package com.example.apv; 

import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
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.MarkerOptions; 
import android.os.Bundle; 
import android.app.Activity; 
import android.app.FragmentManager; 

public class MainActivity extends Activity 
{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

    FragmentManager fragmentManager = getFragmentManager(); 
    MapFragment mapFragment = (MapFragment)fragmentManager.findFragmentById(R.id.map); 
    GoogleMap googleMap = mapFragment.getMap(); 
    LatLng sfLatLng = new LatLng(37.7750, -122.4183); 
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
    googleMap.addMarker(new MarkerOptions().position(sfLatLng).title("San Francisco") 
       .snippet("Population: 776733") 
      .icon(BitmapDescriptorFactory.defaultMarker(
    BitmapDescriptorFactory.HUE_AZURE))); 
    googleMap.getUiSettings().setCompassEnabled(true); 
    googleMap.getUiSettings().setZoomControlsEnabled(true); 
    googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(sfLatLng, 10)) 

     } 
} 

main.xml中

一些環節進行樣品
     <?xml version="1.0" encoding="utf-8"?> 
       <fragment xmlns:android="http://schemas.android.com/apk/res/android" 
          android:id="@+id/map" 
           android:layout_width="match_parent" 
            android:layout_height="match_parent" 
           class="com.google.android.gms.maps.MapFragment"/> 

,最後我的清單文件

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

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


            <!--Required permissions--> 

      permission oid:name="com.codebybrian.mapsample.permission.MAPS_RECEIVE"/> 

       <!--Used by the API to download map tiles from Google Maps servers: --> 
       <uses-permission android:name="android.permission.INTERNET"/> 

        <!--Allows the API to access Google web-based services: --> 
       <uses-permission 
     android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> 

      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 


        <!--Optional permissions--> 
      <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 

     <!--Version 2 of the Google Maps Android API requires OpenGL ES version 2 --> 
       <uses-feature 
         android:glEsVersion="0x00020000"  
          android:required="true"/> 

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

       <activity android:name=".MyMapActivity" 
         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="AZzaSSsBmhi4dXoKSylGGmjkQ5Jev9UdAJBjk"/> 
          </application> 


          </manifest> 

我的17 4.2版本和API級模擬器中運行我的應用程序得到了以下錯誤

12-17 10:06:52.590: E/Trace(826): error opening trace file: No such file or directory (2) 
12-17 10:06:52.590: W/Trace(826): Unexpected value from nativeGetEnabledTags: 0 
12-17 10:06:52.590: W/Trace(826): Unexpected value from nativeGetEnabledTags: 0 
12-17 10:06:52.590: W/Trace(826): Unexpected value from nativeGetEnabledTags: 0 
12-17 10:06:52.680: I/ActivityThread(826): Pub com.google.android.gms.plus;com.google.android.gms.plus.action: com.google.android.gms.plus.provider.PlusProvider 
12-17 10:06:52.740: W/Trace(826): Unexpected value from nativeGetEnabledTags: 0 
12-17 10:06:52.740: W/Trace(826): Unexpected value from nativeGetEnabledTags: 0 
12-17 10:06:52.760: W/Trace(826): Unexpected value from nativeGetEnabledTags: 0 

後來我才知道,這些版本不能在模擬器中執行,所以我試圖執行它與兩個設備之一是索尼xperia你的android版本2.3.7和三星galant選項卡的android版本4.1.1和這些是我的輸出

12-17 16:24:48.965: I/dalvikvm(9088): Could not find method  com.example.apv.MainActivity.getFragmentManager, referenced from method com.example.apv.MainActivity.onCreate 
    12-17 16:24:48.965: W/dalvikvm(9088): VFY: unable to resolve virtual method 3460: Lcom/example/apv/MainActivity;.getFragmentManager()Landroid/app/FragmentManager; 
    12-17 16:24:48.965: D/dalvikvm(9088): VFY: replacing opcode 0x6e at 0x0009 
    12-17 16:24:48.965: D/dalvikvm(9088): VFY: dead code 0x000c-0065 in Lcom/example /apv/MainActivity;.onCreate (Landroid/os/Bundle;)V 
     12-17 16:24:49.066: D/AndroidRuntime(9088): Shutting down VM 
    12-17 16:24:49.066: W/dalvikvm(9088): threadid=1: thread exiting with uncaught exception (group=0x4001d578) 
     12-17 16:24:49.086: E/AndroidRuntime(9088): FATAL EXCEPTION: main 
     12-17 16:24:49.086: E/AndroidRuntime(9088): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.apv/com.example.apv.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment 
     12-17 16:24:49.086: E/AndroidRuntime(9088): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659) 
       12-17 16:24:49.086: E/AndroidRuntime(9088): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675) 
      12-17 16:24:49.086: E/AndroidRuntime(9088): at android.app.ActivityThread.access$1500(ActivityThread.java:121) 
      12-17 16:24:49.086: E/AndroidRuntime(9088): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943) 
      12-17 16:24:49.086: E/AndroidRuntime(9088): at android.os.Handler.dispatchMessage(Handler.java:99) 
      12-17 16:24:49.086: E/AndroidRuntime(9088): at android.os.Looper.loop(Looper.java:138) 
      12-17 16:24:49.086: E/AndroidRuntime(9088): at android.app.ActivityThread.main(ActivityThread.java:3701) 
      12-17 16:24:49.086: E/AndroidRuntime(9088): at java.lang.reflect.Method.invokeNative(Native Method) 
     12-17 16:24:49.086: E/AndroidRuntime(9088): at java.lang.reflect.Method.invoke(Method.java:507) 
      12-17 16:24:49.086: E/AndroidRuntime(9088):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878) 
      12-17 16:24:49.086: E/AndroidRuntime(9088): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636) 
      12-17 16:24:49.086: E/AndroidRuntime(9088): at dalvik.system.NativeStart.main(Native Method) 
      12-17 16:24:49.086: E/AndroidRuntime(9088): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment 
      12-17 16:24:49.086: E/AndroidRuntime(9088): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581) 
       12-17 16:24:49.086: E/AndroidRuntime(9088): at android.view.LayoutInflater.inflate(LayoutInflater.java:386) 
       12-17 16:24:49.086: E/AndroidRuntime(9088): at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 
       12-17 16:24:49.086: E/AndroidRuntime(9088): at android.view.LayoutInflater.inflate(LayoutInflater.java:276) 
       12-17 16:24:49.086: E/AndroidRuntime(9088): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207) 
       12-17 16:24:49.086: E/AndroidRuntime(9088): at android.app.Activity.setContentView(Activity.java:1657) 
       12-17 16:24:49.086: E/AndroidRuntime(9088): at com.example.apv.MainActivity.onCreate(MainActivity.java:20) 
       12-17 16:24:49.086: E/AndroidRuntime(9088): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
       12-17 16:24:49.086: E/AndroidRuntime(9088): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623) 
        12-17 16:24:49.086: E/AndroidRuntime(9088): ... 11 more 
      12-17 16:24:49.086: E/AndroidRuntime(9088): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.apv-1.apk] 
       12-17 16:24:49.086: E/AndroidRuntime(9088): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240) 
       12-17 16:24:49.086: E/AndroidRuntime(9088): at java.lang.ClassLoader.loadClass(ClassLoader.java:551) 
       12-17 16:24:49.086: E/AndroidRuntime(9088):  at java.lang.ClassLoader.loadClass(ClassLoader.java:511) 
        12-17 16:24:49.086: E/AndroidRuntime(9088): at android.view.LayoutInflater.createView(LayoutInflater.java:471) 
        12-17 16:24:49.086: E/AndroidRuntime(9088): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549) 
       12-17 16:24:49.086: E/AndroidRuntime(9088): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66) 
       12-17 16:24:49.086: E/AndroidRuntime(9088):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568) 
        12-17 16:24:49.086: E/AndroidRuntime(9088): ... 19 more 

可以在任何一個請建議我如何完成這件事,給我的谷歌地圖的新版本API第2版教程一些鏈接和一些例子鏈接,請幫我

+0

請正確縮進代碼,以使其更具可讀性。並查看https://github.com/apacha/maps_demo以獲得簡約地圖示例。有關更好的示例,請參閱'Android SDK \ extras \ google \ google_play_services \ samples \ maps' –

回答

0

一定要命名您的主要活動爲你聲明它在清單中:

Caused by: java.lang.ClassNotFoundException: com.example.apv.MyMapActivity 

也許包命名問題?發佈您的整個MyMapActivity,不僅是onCreate。

+0

我已經更新了一個 – bapatla

+0

地圖訪問權限必須以您的包名稱開頭。用' protectionLevel =「簽名」/>' –

-5

如果你想使用片段那麼你的活動即你MainActivity應該延伸FragmentActivity

+0

不正確。 Google的Google文檔明確指出,否則。 –

0

您提供的代碼包含錯誤。您的活動名稱爲MainActivity,但在清單文件中,您聲明的唯一活動具有名稱MyMapActivity。你的清單文件也沒有開放的應用程序標籤。

更正這兩個錯誤,我已經運行你的代碼,並得到你提到的異常。我通過添加google-play-services庫項目來解決問題。我想你忘了添加或未正確添加。你可以在谷歌地圖v2的開發者文檔中找到它的細節。

要檢查此項,請右鍵單擊您的項目並轉至屬性。然後從左側選擇Android。在這個頁面的底部,你會發現庫項目表。確保google-play-services庫項目位於列表中並具有綠色標記。如果不存在,請單擊右側的「添加..」按鈕進行添加。

有時,eclipse無法添加庫項目,它不在項目的同一工作區中。如果這是您的情況,請將google-play-services和您的項目放在同一個工作區中,然後重試。

0

看看這個YouTube教程視頻。這是初學者,並將幫助您在創建一個簡單的谷歌地圖V2應用程序從零開始youtube link。而據Eclipse的模擬器運行谷歌地圖V2而言看到這個答案Running Google Maps v2 on emulator