So, I'm trying to create a DrawerLayout that starts a map fragment when you push a button. Everything I've tried either throws a backstack exception or nullpointer. Here's what I have so far.
activity_main.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_view"
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
<!--The drawer-->
<ListView android:id="@+id/drawer_view"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#9E9E9E"/>
</android.support.v4.widget.DrawerLayout>
geo_fence.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/map"
android:layout_height="match_parent"
android:layout_width="match_parent"
class="com.google.android.gms.maps.MapFragment"/>
</LinearLayout>
MainActivity.java
import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import java.util.ArrayList;
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private SharedPreferences mPreferences;
private JSONManager jsonManager;
private PlacesListAdapter adapter;
private Place currentPlace = null;
private GoogleMap gmap = null;
final int RQS_GooglePlayServices = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Construct the action bar
ActionBar ab = getActionBar();
ab.setHomeButtonEnabled(true);
ab.setDisplayHomeAsUpEnabled(true);
//Construct the slide out drawer
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.drawer_view);
createButtons();
}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch(menuItem.getItemId()) {
case android.R.id.home:
mDrawerLayout.openDrawer(mDrawerList);
case R.id.new_action:
mDrawerLayout.openDrawer(mDrawerList);
case R.id.remove_place:
adapter.remove(currentPlace);
mDrawerLayout.openDrawer(mDrawerList);
}
return (super.onOptionsItemSelected(menuItem));
}
@Override
public void onResume() {
super.onResume();
if (mPreferences.getBoolean("firstrun", true)) {
//FIX THIS - needs to run to check for play services
ProgressDialog showWait = new ProgressDialog(this);
showWait.setTitle("Initializing");
showWait.setMessage("Please wait, checking for Google Play Services");
showWait.show();
startActivity(new Intent(this, CheckServices.class));
showWait.dismiss();
//FIX THIS
firstRun();
}
}
/*
Display a map
*/
private void addPlacesPressed() {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
MapFragment map = (MapFragment)fm.findFragmentById(R.id.map);
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this.getApplicationContext());
if (resultCode == ConnectionResult.SUCCESS){
if(gmap == null){
gmap = map.getMap();
if(gmap != null){
//myMap.setMyLocationEnabled(true);
gmap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
}else{
Toast.makeText(this.getApplicationContext(),
"cannot getMap!",
Toast.LENGTH_LONG).show();
}
}
}else{
GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices);
}
ft = fm.beginTransaction();
ft.replace(R.id.content_view, map, "map");
ft.commit();
mDrawerLayout.closeDrawer(mDrawerList);
}
private void createButtons() {
Button addPlacesButton = new Button(this);
addPlacesButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
addPlacesPressed();
}
}
);
mDrawerList.addView(addPlacesButton);
}
}
Where I try to execute the getMap() against the map Fragment it just throws a nullpointer. I have all the necessary things added to the manifest, I've gotten this working if I start the map when the application starts. It just fails when I try to replace the FramLayout with a MapFragment that's in a LinearLayout.