I am stuck with a drawer layout where I try to implement google maps when pressed on drawer item 1. It's something similar like what I found here: Google Maps V2 + Drawer Layout
I tried to reproduce what he did, and it pushed me in the right direction.
UPDATED
I have moved "setUpMapIfNeeded" and "SetUpMap" to the MapFragment and removed static from the class. Google maps works now, but crashes after trying to navigate to the fragment in the drawer navigation.
Does somebody know how to help me with this? I very much appreciate it!
The Basic stuff
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
mMenuTitles = getResources().getStringArray(R.array.menu_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mMenuTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
My On Select
Fragment mapFragment;
Fragment fragment;
private void selectItem(int position) {
FragmentManager fragmentManager = getFragmentManager();
if (position == 0) {
if (mapFragment == null) {
mapFragment = new MapFragment();
}
Bundle args = new Bundle();
args.putInt(MapFragment.ARG_NUMBER, position);
mapFragment.setArguments(args);
fragmentManager.beginTransaction().replace(R.id.content_frame, mapFragment).commit();
} else if (position == 1) {
if (fragment == null) {
fragment = new InformationFragment();
}
Bundle args = new Bundle();
args.putInt(InformationFragment.ARG_NUMBER, position);
fragment.setArguments(args);
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
}
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
My MapFragments (drawer item 1)
public class MapFragment extends Fragment {
public static final String ARG_NUMBER = "arg_number";
public MapFragment() {
// Empty constructor required for fragment subclasses
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
LatLng museum = new LatLng(52.360011, 4.885216);
mMap.setBuildingsEnabled(true);
mMap.setMyLocationEnabled(false);
// Construct a CameraPosition focusing on Mountain View and animate the camera to that position.
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(museum) // Sets the center of the map to Mountain View
.zoom(19) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
CameraUpdate upd = CameraUpdateFactory.newLatLngZoom(new LatLng(52.360011, 4.885216), 18);
mMap.moveCamera(upd);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.maps_fragment, container, false);
int i = getArguments().getInt(ARG_NUMBER);
String title = getResources().getStringArray(R.array.menu_array)[i];
setUpMapIfNeeded();
getActivity().setTitle(title);
return rootView;
}
}
My information_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingTop="20dp"
android:text="Title 2"
android:textStyle="bold" />
<TextView
android:id="@+id/textViewDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingRight="20dp"
android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." />
My maps_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="0dp"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_weight="0.29" />
Try modifying your code like add fragmentManager.executePendingTransactions(); after commit
Fragment mapFragment;
Fragment fragment;
private void selectItem(int position) {
FragmentManager fragmentManager = getFragmentManager();
if (position == 0) {
if (mapFragment == null) {
mapFragment = new MapFragment();
}
Bundle args = new Bundle();
args.putInt(MapFragment.ARG_NUMBER, position);
mapFragment.setArguments(args);
fragmentManager.beginTransaction().replace(R.id.content_frame, mapFragment).commit();
} else if (position == 1) {
if (fragment == null) {
fragment = new InformationFragment();
}
Bundle args = new Bundle();
args.putInt(InformationFragment.ARG_NUMBER, position);
fragment.setArguments(args);
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
}
fragmentManager.executePendingTransactions();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.