抱歉耽搁了,但这是主要活动:
public class MainActivity extends FragmentActivity implements
ActionBar.OnNavigationListener , LocationListener{
private String [] types;
private ActionBar actionBar;
public static GoogleMap map = null;
public static boolean googlePlayOn = false;
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
types = getResources().getStringArray(R.array.values);
googlePlayOn = isGooglePlay();
actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
// Set up the dropdown list navigation in the action bar.
actionBar.setListNavigationCallbacks(
// Specify a SpinnerAdapter to populate the dropdown list.
new ArrayAdapter<String>(getActionBarThemedContextCompat(),android.R.layout.simple_list_item_1,android.R.id.text1, types), this);
}
/**
* This function checks in the phone operating system that
* the phone got google play services
* @return
*/
private boolean isGooglePlay()
{
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if(status == ConnectionResult.SUCCESS)
return true;
else
((Dialog) GooglePlayServicesUtil.getErrorDialog(status, this, 10)).show();
return false;
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private Context getActionBarThemedContextCompat() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
return getActionBar().getThemedContext();
} else {
return this;
}
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
// Restore the previously serialized current dropdown position.
if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
getActionBar().setSelectedNavigationItem(
savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
// Serialize the current dropdown position.
outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar()
.getSelectedNavigationIndex());
}
@Override
public boolean onNavigationItemSelected(int position, long id) {
// When the given dropdown item is selected, show its contents in the
// container view.
if(position != PlacesFinder.currentTitle)
{
if(!PlacesFinder.created)
{
PlacesFinder.created = true;
Bundle b = new Bundle();
b.putInt(PlacesFinder.ARG_SECTION_NUMBER, position);
Fragment fragment = new PlacesFinder();
fragment.setArguments(b);
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
}
PlacesFinder.newTitleBeenChosenHandler.sendEmptyMessage(0);
}
return true;
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class PlacesFinder extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static boolean created = false;
public static final String ARG_SECTION_NUMBER = "section_number";
public static Handler newTitleBeenChosenHandler;
public static int currentTitle = 0;
private Context context;
public PlacesFinder() {
this.context = getActivity();
loadMapFragment();
}
private void loadMapFragment()
{
if(googlePlayOn)
{
if(map == null)
map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.places_fragment, container , false);
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
}
This is the main xml file :
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" />
这是地图片段文件:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
一旦我做了这行代码 map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
应用程序失败,它说我有一个空指针,我不知道为什么......