2014-03-01 47 views
0

我遇到以下問題: 當我按下按鈕時,我的主要活動啓動了一個FragmentActivity,這意味着要使用Google庫來顯示以第一個活動傳遞的座標爲中心的地圖。 現在,我在FragmentActivity中放了幾條Log.i行,看起來它的onCreate方法被調用,並且在嘗試設置活動的ContentView時發生崩潰。 這裏的代碼,我叫FragmentActivity塊,稱爲ShowMap:啓動GoogleMaps時應用程序崩潰FragmentActivity

mbtMap.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View v){ 
      Intent aprimappa = new Intent(MyCoordinates.this,ShowMap.class); 
      Bundle catasta = new Bundle(); 
      catasta.putDouble("lat", Lati); 
      catasta.putDouble("lon", Longi); 
      aprimappa.putExtras(catasta); 
      Log.i(TAG, "Intento preparato. Chiamo ShowMap."); 
      startActivity(aprimappa); 
     } 
    }); 

這是我的

ShowMap.class的第一位:

public class ShowMap extends FragmentActivity{ 

final static String TAG="thereyouwere"; 

private GoogleMap googlemap; 

@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    Log.i(TAG, "Siamo nell'onCreate di ShowMap."); 
    setContentView(R.layout.activity_show_map); 
    Log.i(TAG,"Caricato layout di ShowMap."); 
    Bundle b = getIntent().getExtras(); 
    double lat=b.getDouble("lat"); 
    double lon=b.getDouble("lon"); 

如果您需要它,這裏也是我的AndroidManifest.XML和xml佈局

ShowMap:

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


<!-- Google Maps API Key --> 
<meta-data 
android:name="com.google.android.maps.v2.API_KEY" 
android:value="***********************" /> 

<permission 
    android:name="it.paquito.thereyouwere.permission.MAPS_RECEIVE" 
    android:protectionLevel="signature" /> 
<uses-sdk 
    android:minSdkVersion="11" 
    android:targetSdkVersion="19" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
<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-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="it.paquito.thereyouwere.MyCoordinates" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name="ShowMap"></activity> 
</application> 

</manifest> 

<?xml version="1.0" encoding="utf-8"?> 
<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/fragment1" 
    android:name="com.google.android.gms.maps.MapFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

預先感謝您的任何幫助,您可以給我!

+0

能否請您補充提logcat的? – donfuxx

回答

0

首先把你的Maps API鍵下<application>標籤在manifest.xml

....<application> 
<meta-data 
android:name="com.google.android.maps.v2.API_KEY" 
android:value="***********************" /> 
</application> 

你可能需要這改變了,因爲你的minsdkversion<12和較低的版本(預蜂窩)設備使用SupportMapFragment ..

,你應該用SupportMapFragment代替MapFragment變更

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/fragment1" 
android:name="com.google.android.gms.maps.MapFragment" 
android:layout_width="match_parent" 
android:layout_height="match_parent" /> 

隨着

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

而且改變

_googleMap=((MapFragment)getFragmentManager().findFragmentById(R.id.fragment1)).getMap(); 

隨着

_googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
    R.id.fragment1)).getMap(); 

,並檢查

添加谷歌Play服務版本,您的應用程序的清單

你只需要添加<meta-data><application>代碼到你的AndroidManifest.xml

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

這是因爲谷歌最新的播放服務,需要一個版本的名稱,這是使用<meta-data .. />AndroidManifest.xml