如何从导航抽屉调用片段活动
how to call FragmentActivitie from navegation drawer
我想在第一种情况下显示地图,这是调用地图片段时 DrawerLayout 的一部分。这让我出错,我不知道该怎么做。
@Override
public void onNavigationDrawerItemSelected(int position) {
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment =null;
switch (position){
case 0:
fragment = new MapsActivity(); //this line show error
break;
case 1:
//to implement
break;
case 2:
//to implement
break;
}
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
这是 google 地图的代码 Activity:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
LatLng minorca = new LatLng(40.001568, 3.837685);
map.addMarker(new MarkerOptions().position(minorca).title("Minorca"));
map.moveCamera(CameraUpdateFactory.newLatLng(minorca));
}
}
希望你能帮助我。谢谢!
那是因为 navigation drawer
需要使用片段,但是你使用了 activity 所以导致了错误。
解决方案是您需要扩展 Fragment
,您需要将 MapsActivity
更改为 MapsFragment
,如下所示:
public class MapsFragment extends Fragment {
private MapView mapView;
public static MapsFragment newInstance() {
MapsFragment fragment = new MapsFragment();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.mapview, container, false);
// Gets the MapView from the XML layout and creates it
MapsInitializer.initialize(getActivity());
switch (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity())) {
case ConnectionResult.SUCCESS:
Toast.makeText(getActivity(), "SUCCESS", Toast.LENGTH_SHORT).show();
mapView = (MapView) v.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
// Gets to GoogleMap from the MapView and does initialization stuff
if (mapView != null) {
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.setMyLocationEnabled(true);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
googleMap.animateCamera(cameraUpdate);
}
});
}
break;
case ConnectionResult.SERVICE_MISSING:
Toast.makeText(getActivity(), "SERVICE MISSING", Toast.LENGTH_SHORT).show();
break;
case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
Toast.makeText(getActivity(), "UPDATE REQUIRED", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(getActivity(), GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()), Toast.LENGTH_SHORT).show();
}
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();
}
}
布局 xml 文件是:
<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
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name=".MapsFragment"/>
</RelativeLayout>
请注意,您的 DrawerLayout
文件和 MapsFragment
.
都应该 import android.support.v4.app.Fragment;
终于用上了
fragment = MapsFragment.newInstance();
而不是
fragment = new MapsActivity(); //this line show error
在你的 DrawerLayout
.
我想在第一种情况下显示地图,这是调用地图片段时 DrawerLayout 的一部分。这让我出错,我不知道该怎么做。
@Override
public void onNavigationDrawerItemSelected(int position) {
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment =null;
switch (position){
case 0:
fragment = new MapsActivity(); //this line show error
break;
case 1:
//to implement
break;
case 2:
//to implement
break;
}
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
这是 google 地图的代码 Activity:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
LatLng minorca = new LatLng(40.001568, 3.837685);
map.addMarker(new MarkerOptions().position(minorca).title("Minorca"));
map.moveCamera(CameraUpdateFactory.newLatLng(minorca));
}
}
希望你能帮助我。谢谢!
那是因为 navigation drawer
需要使用片段,但是你使用了 activity 所以导致了错误。
解决方案是您需要扩展 Fragment
,您需要将 MapsActivity
更改为 MapsFragment
,如下所示:
public class MapsFragment extends Fragment {
private MapView mapView;
public static MapsFragment newInstance() {
MapsFragment fragment = new MapsFragment();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.mapview, container, false);
// Gets the MapView from the XML layout and creates it
MapsInitializer.initialize(getActivity());
switch (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity())) {
case ConnectionResult.SUCCESS:
Toast.makeText(getActivity(), "SUCCESS", Toast.LENGTH_SHORT).show();
mapView = (MapView) v.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
// Gets to GoogleMap from the MapView and does initialization stuff
if (mapView != null) {
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.setMyLocationEnabled(true);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
googleMap.animateCamera(cameraUpdate);
}
});
}
break;
case ConnectionResult.SERVICE_MISSING:
Toast.makeText(getActivity(), "SERVICE MISSING", Toast.LENGTH_SHORT).show();
break;
case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
Toast.makeText(getActivity(), "UPDATE REQUIRED", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(getActivity(), GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()), Toast.LENGTH_SHORT).show();
}
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();
}
}
布局 xml 文件是:
<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
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name=".MapsFragment"/>
</RelativeLayout>
请注意,您的 DrawerLayout
文件和 MapsFragment
.
import android.support.v4.app.Fragment;
终于用上了
fragment = MapsFragment.newInstance();
而不是
fragment = new MapsActivity(); //this line show error
在你的 DrawerLayout
.