2013-10-14 64 views
0

我有一個MainActivity,它有一個帶有兩個選項卡的ActionBar,第一個選項卡用於顯示傳感器數據值,第二個選項卡用於顯示Map。 但是我有一個問題,以實現在片段地圖中的第二個標籤上如何在片段中實現谷歌地圖

在MainActivity如下所示:

public class MainActivity extends Activity 
{ 
private static final String DATA_FRAGMENT_BAR = "Data"; 
private static final String MAP_FRAGMENT_BAR = "Map"; 

private DataCollection mDataCollection; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Config.mContext = this; 
    ActionBar actionBar = getActionBar(); 
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME| ActionBar.DISPLAY_SHOW_CUSTOM); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

    ActionBar.Tab dataTab = actionBar.newTab().setText(DATA_FRAGMENT_BAR); 
    ActionBar.Tab mapTab = actionBar.newTab().setText(MAP_FRAGMENT_BAR); 
    Fragment dataFragment = new FragmentData(); 
    Fragment mapFragment = new FragmentMap(); 

    dataTab.setTabListener(new MyTabsListener(dataFragment)); 
    mapTab.setTabListener(new MyTabsListener(mapFragment)); 

    actionBar.addTab(dataTab); 
    actionBar.addTab(mapTab); 
    mDataCollection = DataCollection.getInstance(); 
} 
.... 
} 

的TabsListener如下所示 公共類MyTabsListener實現ActionBar.TabListener 私人片段片段;

public MyTabsListener(Fragment fragment) 
{ 
    this.fragment = fragment; 
} 

@Override 
public void onTabReselected(Tab tab, FragmentTransaction ft) { 
    // TODO Auto-generated method stub 
} 

@Override 
public void onTabSelected(Tab tab, FragmentTransaction ft) { 
    // TODO Auto-generated method stub 
    ft.replace(R.id.fragment_container, fragment); 
} 

@Override 
public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
    // TODO Auto-generated method stub 
    ft.remove(fragment); 
} 
} 

的地圖片段類如下所示:

public class FragmentMap extends Fragment 
{ 

MapView mapView; 
GoogleMap map; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
    View v = inflater.inflate(R.layout.fragment_map, container, false); 
    // Gets the MapView from the XML layout and creates it 
    mapView = (MapView) v.findViewById(R.id.mapView); 
    mapView.onCreate(savedInstanceState); 

    // Gets to GoogleMap from the MapView and does initialization stuff 
    map = mapView.getMap(); 
    //map.getUiSettings().setMyLocationButtonEnabled(false); 
    //map.setMyLocationEnabled(true); 
    map.addMarker(new MarkerOptions().position(new LatLng(50.167003,19.383262))); 


    // Needs to call MapsInitializer before doing any CameraUpdateFactory calls 
    try 
    { 
     MapsInitializer.initialize(this.getActivity()); 
    } 
    catch (GooglePlayServicesNotAvailableException e) 
    { 
     e.printStackTrace(); 
    } 

    // Updates the location and zoom of the MapView 
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10); 
    map.animateCamera(cameraUpdate); 

    return v; 
} 

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

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

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

我設置在該線的FragmentMap類斷點 「mapView.onCreate(savedInstanceState);」 savedInstanceState爲空,程序將在行後崩潰。

有人可以幫忙。提前致謝。

回答

0

谷歌發佈的Map API版本2.這給你一個MapFragment和一個SupportMapFragment。這允許您添加一個Map而不擴展MapActivity。

Google Maps v2 In fragment cashes when clicked twice

+1

我有添加的問題TabsListener類。我的問題是如果我使用MapFragment,TabsListener將不起作用。我有兩個選項卡,其中一個選項卡用於顯示傳感器值,因此我使用了Fragment – user2753594

0

它在文檔中沒有真正清楚,但MapView.onCreate()方法應該在Fragment.onActivityCreated()叫,不Fragment.onCreate()

此外,MapView.getMap()可以返回null,不要忘了檢查一下。

而且你應該叫super.onResume()mapView.onResume()