2013-08-24 27 views
0

我試圖實現一個應用程序使用地圖api v2的android。 這裏是我使用Android地圖v2僅API縮放控件可見

<?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" 
      android:name="com.google.android.gms.maps.SupportMapFragment"/> 

主要活動代碼

package com.example.nav3; 

import android.os.Bundle; 
import android.app.Activity; 
import android.support.v4.app.FragmentActivity; 
import android.view.Menu; 

public class MainActivity2 extends FragmentActivity { 

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

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main_activity2, menu); 
     return true; 
    } 

} 

清單是這個

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

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

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

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

    <uses-permission android:name="com.example.nav3.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="com.example.nav3.MainActivity2" 
      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 key comes here" /> 
    </application> 

</manifest> 

在雖然在運行的應用程序,我只看到了縮放控制,但沒有地圖是可見的。這是爲什麼?由於

堆棧跟蹤

08-24 22:58:41.482: D/AndroidRuntime(3083): Shutting down VM 
08-24 22:58:41.482: W/dalvikvm(3083): threadid=1: thread exiting with uncaught exception (group=0x41552700) 
08-24 22:58:41.502: D/dalvikvm(3083): GC_FOR_ALLOC freed 285K, 4% free 9093K/9404K, paused 15ms, total 15ms 
08-24 22:58:41.512: E/AndroidRuntime(3083): FATAL EXCEPTION: main 
08-24 22:58:41.512: E/AndroidRuntime(3083): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nav3/com.example.nav3.MainActivity2}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment 
08-24 22:58:41.512: E/AndroidRuntime(3083):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 
08-24 22:58:41.512: E/AndroidRuntime(3083):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
08-24 22:58:41.512: E/AndroidRuntime(3083):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
08-24 22:58:41.512: E/AndroidRuntime(3083):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
08-24 22:58:41.512: E/AndroidRuntime(3083):  at android.os.Handler.dispatchMessage(Handler.java:99) 
08-24 22:58:41.512: E/AndroidRuntime(3083):  at android.os.Looper.loop(Looper.java:137) 
08-24 22:58:41.512: E/AndroidRuntime(3083):  at android.app.ActivityThread.main(ActivityThread.java:5103) 
08-24 22:58:41.512: E/AndroidRuntime(3083):  at java.lang.reflect.Method.invokeNative(Native Method) 
08-24 22:58:41.512: E/AndroidRuntime(3083):  at java.lang.reflect.Method.invoke(Method.java:525) 
08-24 22:58:41.512: E/AndroidRuntime(3083):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
08-24 22:58:41.512: E/AndroidRuntime(3083):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
08-24 22:58:41.512: E/AndroidRuntime(3083):  at dalvik.system.NativeStart.main(Native Method) 
08-24 22:58:41.512: E/AndroidRuntime(3083): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment 
08-24 22:58:41.512: E/AndroidRuntime(3083):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713) 
08-24 22:58:41.512: E/AndroidRuntime(3083):  at android.view.LayoutInflater.inflate(LayoutInflater.java:469) 
08-24 22:58:41.512: E/AndroidRuntime(3083):  at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
08-24 22:58:41.512: E/AndroidRuntime(3083):  at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
08-24 22:58:41.512: E/AndroidRuntime(3083):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267) 
08-24 22:58:41.512: E/AndroidRuntime(3083):  at android.app.Activity.setContentView(Activity.java:1895) 
08-24 22:58:41.512: E/AndroidRuntime(3083):  at com.example.nav3.MainActivity2.onCreate(MainActivity2.java:13) 
08-24 22:58:41.512: E/AndroidRuntime(3083):  at android.app.Activity.performCreate(Activity.java:5133) 
08-24 22:58:41.512: E/AndroidRuntime(3083):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
08-24 22:58:41.512: E/AndroidRuntime(3083):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 
08-24 22:58:41.512: E/AndroidRuntime(3083):  ... 11 more 
08-24 22:58:41.512: E/AndroidRuntime(3083): Caused by: java.lang.IllegalStateException: Fragment com.google.android.gms.maps.MapFragment did not create a view. 
08-24 22:58:41.512: E/AndroidRuntime(3083):  at android.app.Activity.onCreateView(Activity.java:4769) 
08-24 22:58:41.512: E/AndroidRuntime(3083):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689) 
08-24 22:58:41.512: E/AndroidRuntime(3083):  ... 20 more 
+1

請張貼您的清單,並請檢查LogCat的任何相關錯誤。 – CommonsWare

+0

請檢查更新的問題 –

+0

@ Shivam10,您不需要使用'SupportMapFragment'。使用'MapFragment'。因爲你min sdk是17而不低於12 – Raghunandan

回答

0

您需要使用MapFragmentSupportMapFragment

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

你也需要擴展標準Activity

確保您有正確的import語句

+0

這兩種情況下的應用程序崩潰。 –

+0

@ Shivam10你需要發佈堆棧跟蹤。還需要進行建議的更改。 – Raghunandan

+0

是的,我已經將supportmapfragment更改爲mapfragment和fragmentactivity,但在這種情況下應用程序崩潰。顯示的錯誤是「錯誤膨脹類碎片」 –

0
如果您使用證書籤署有關申請API密鑰在谷歌API控制檯中生成

檢查(確保您使用的是Google Maps Android API v2而不是Google Maps API v2)。

請確保您正在測試您的應用的設備支持OpenGL ES 2.0。

+0

是的,我已經檢查了這兩件事 –

0

檢查正在使用的所有權限不 這些下列權限

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

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

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

,並檢查wheather API密鑰有效檢查您設備的網絡連接。

+0

是的,我已檢查所有這些東西。 –

+0

並檢查谷歌谷歌播放服務沒有。 com.google.android.gms.maps。MapFragment 使用這個使用另一個設備它可能會發生。 如果谷歌播放服務沒有在設備上安裝,它會帶來 –

+0

檢查谷歌API API是否gmaps API的V2服務? –

相關問題