1

這裏是我想添加一個谷歌API到一個操作欄去一個片段的情況。但是,它失敗了,因爲片段類必須在操作欄不允許的情況下擴展FragmentActivity,任何人都可以幫助修復它?非常感謝!如何將Google Map API v2包含到片段中?

MainHome.java

package com.example.demo3;  
import android.app.ActionBar; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 

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

     initView(); 
    } 


    private void initView() { 

     final ActionBar actionBar = getActionBar(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
     actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE); 

     actionBar.addTab(actionBar 
       .newTab() 
       .setText("My Information") 
       .setTabListener(
         new MyTabListener<FragmentPage1>(this, 
           FragmentPage1.class))); 

     actionBar.addTab(actionBar 
       .newTab() 
       .setText("My Location") 
       .setTabListener(
         new MyTabListener<FragmentPage2>(this, 
           FragmentPage2.class))); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.prelog, menu); 
     return true; 
    } 
} 

Fragment2.java

package com.example.demo3; 

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

import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.BitmapDescriptorFactory; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.Marker; 
import com.google.android.gms.maps.model.MarkerOptions; 

public class FragmentPage2 extends Fragment { 

     static final LatLng HAMBURG = new LatLng(53.558, 9.927); 
     static final LatLng KIEL = new LatLng(53.551, 9.993); 
     private GoogleMap map; 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View v = inflater.inflate(R.layout.locationhome, null, false); 

     map = ((SupportMapFragment) getFragmentManager().findFragmentById(
       R.id.map)).getMap(); 

     Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG) 
       .title("Hamburg")); 
     Marker kiel = map.addMarker(new MarkerOptions() 
       .position(KIEL) 
       .title("Kiel") 
       .snippet("Kiel is cool") 
       .icon(BitmapDescriptorFactory 
         .fromResource(R.drawable.ic_launcher))); 

     // Move the camera instantly to hamburg with a zoom of 15. 
     map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15)); 

     // Zoom in, animating the camera. 
     map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null); 

     return v; 

    } 
} 

它帶給我需要延長FragmentActivity使

map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 

。但是,它會使

actionBar.addTab(actionBar 
         .newTab() 
         .setText("My Location") 
         .setTabListener(
           new MyTabListener<FragmentPage2>(this, 
             FragmentPage2.class))); 

失敗。任何人都可以有一些建議?

非常感謝!

回答

3

使用MapView的,而不是像這樣

public class MyMapFragment extends Fragment { 

    private MapView mMapView; 
    private GoogleMap mMap; 
    private Bundle mBundle; 

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

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

     mMapView = (MapView) inflatedView.findViewById(R.id.map); 
     mMapView.onCreate(mBundle); 
     setUpMapIfNeeded(inflatedView); 

     return inflatedView; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mBundle = savedInstanceState; 
    } 

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

    private void setUpMap() { 
     mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); 
    } 

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

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

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

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

和修改Java文件mapfragment

相關問題