Java 如何将GoogleMapAPIv2包含到片段中?
这里是一个例子,我想在一个动作栏中添加一个GoogleAPI,该动作栏将指向一个片段。但是,它失败了,因为fragment类必须扩展FragmentActivity,而操作栏不允许它,有人可以帮助修复它吗?非常感谢 MainHome.javaJava 如何将GoogleMapAPIv2包含到片段中?,java,android,google-maps,android-fragments,android-actionbar,Java,Android,Google Maps,Android Fragments,Android Actionbar,这里是一个例子,我想在一个动作栏中添加一个GoogleAPI,该动作栏将指向一个片段。但是,它失败了,因为fragment类必须扩展FragmentActivity,而操作栏不允许它,有人可以帮助修复它吗?非常感谢 MainHome.java package com.example.demo3; import android.app.ActionBar; import android.app.Activity; import android.os.Bundle; import andro
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;
}
}
它带来了我需要扩展FragmentActivity以启用
map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
。然而,这将使
actionBar.addTab(actionBar
.newTab()
.setText("My Location")
.setTabListener(
new MyTabListener<FragmentPage2>(this,
FragmentPage2.class)));
actionBar.addTab(actionBar
.newTab()
.setText(“我的位置”)
.SetTableListener(
新MyTabListener(此,
碎片(第2页,类别));
失败了。谁能给点建议
非常感谢 使用mapview而不是像这样使用mapfragment
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
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();
}
}
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();
}
}