2014-06-07 51 views
0

我誤差範圍內使用的片段時MapView類使用我的MapView片段中下APIV2下谷歌地圖API的Android 2.0

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

import com.google.android.gms.common.GooglePlayServicesNotAvailableException; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.MapView; 
import com.google.android.gms.maps.MapsInitializer; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

public class MapFragment extends Fragment { 

    private MapView mMapView; 
    private GoogleMap mMap; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View inflatedView = inflater.inflate(R.layout.map, container, false); 

     // try { 
       // MapsInitializer.initialize(getActivity()); 
     // } catch (GooglePlayServicesNotAvailableException e) { 
       // TODO handle this situation 
     // } 


     mMapView = (MapView) inflatedView.findViewById(R.id.mapView); 
     mMapView.onCreate(savedInstanceState); 
     setUpMapIfNeeded(inflatedView); 
     mMapView.onResume(); 

    // try { 
     // MapsInitializer.initialize(getActivity()); 
    // } catch (GooglePlayServicesNotAvailableException e) { 
    //  e.printStackTrace(); 
    // } 

     mMap = mMapView.getMap(); 
     return inflatedView; 
    } 


    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

    } 

    private void setUpMapIfNeeded(View inflatedView) { 
     if (mMap == null) { 
      mMap = ((MapView) inflatedView.findViewById(R.id.mapView)).getMap(); 
      if (mMap != null) { 
       setUpMap(); 
      } 
     } 
    } 

    private void setUpMap() { 
     mMap.addMarker(new MarkerOptions().position(new LatLng(55,-4)).title("title")); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     mMapView.onResume(); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     mMapView.onPause(); 
    } 

    @Override 
    public void onDestroy() { 
     mMapView.onDestroy(); 
     super.onDestroy(); 
    } 
    @Override 
    public void onLowMemory() { 
     super.onLowMemory(); 
     mMapView.onLowMemory(); 
    } 

} 

這裏是我的map.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" 
> 


<com.google.android.gms.maps.MapView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:map="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/mapView" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
    /> 



    <!-- 
<fragment 
android:id="@+id/mapView" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
class="com.google.android.gms.maps.MapFragment" 
/> 
    --> 
</RelativeLayout> 

發生錯誤於:** mMapView.onCreate(savedInstanceState); ** 然後,如果我uncommand

`// try { 
       // MapsInitializer.initialize(getActivity()); 
     // } catch (GooglePlayServicesNotAvailableException e) { 
       // TODO handle this situation 
     // }` 

錯誤將在這裏:GooglePlayServicesNotAvailableException 它說這個

unresearchable catch塊需要他人的幫助

+0

你生成了API密鑰 –

+0

那麼如果代碼運行在「GooglePlayServicesNotAvailableException」的catch語句中,這隻表示Play服務太舊或未安裝在該設備上。 – rekire

+0

我已經生成API密鑰 – YDev

回答

0

變化的這部分代碼:

} catch (GooglePlayServicesNotAvailableException e) { 

到:

}catch (Exception e) { 
    throw new RuntimeException(e); 
} 
+0

然後錯誤發生在:mMapView .onCreate(savedInstanceState);標籤:android運行時 – YDev