Java 对于MainActivity:android类型,未定义getSupportFragmentManager()方法

Java 对于MainActivity:android类型,未定义getSupportFragmentManager()方法,java,android,eclipse,google-maps,Java,Android,Eclipse,Google Maps,我收到以下错误:类型MainActivity的getSupportFragmentManager方法未定义。当我将声明更改为: SupportMapFragment fm = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map); 然后我得到了这个错误: Multiple markers at this line - Cannot cast from Fragment to SupportMapFragme

我收到以下错误:类型MainActivity的getSupportFragmentManager方法未定义。当我将声明更改为:

SupportMapFragment fm = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
然后我得到了这个错误:

Multiple markers at this line
- Cannot cast from Fragment to 
 SupportMapFragment
- Line breakpoint:MainActivity [line: 52] - 
 onCreate(Bundle)
源代码:

 package in.wptrafficanalyzer.proximitymapv2;

import android.app.Activity;
import android.app.Dialog;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color; 
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends Activity {

GoogleMap googleMap;
LocationManager locationManager;
PendingIntent pendingIntent;
SharedPreferences sharedPreferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 


    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

    if(status!=ConnectionResult.SUCCESS){ 

        int requestCode = 10;
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
        dialog.show();

    }else {             

        SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

        googleMap = fm.getMap();

        googleMap.setMyLocationEnabled(true);           


        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);


        sharedPreferences = getSharedPreferences("location", 0);

        String lat = sharedPreferences.getString("lat", "0");

        String lng = sharedPreferences.getString("lng", "0");

        String zoom = sharedPreferences.getString("zoom", "0");

        if(!lat.equals("0")){

            drawCircle(new LatLng(Double.parseDouble(lat), Double.parseDouble(lng)));

            drawMarker(new LatLng(Double.parseDouble(lat), Double.parseDouble(lng)));

            googleMap.moveCamera(CameraUpdateFactory.newLatLng(new                LatLng(Double.parseDouble(lat), Double.parseDouble(lng))));

            googleMap.animateCamera(CameraUpdateFactory.zoomTo(Float.parseFloat(zoom)));                


        }


        googleMap.setOnMapClickListener(new OnMapClickListener() {

            @Override
            public void onMapClick(LatLng point) {

                googleMap.clear();      

                drawMarker(point);

                drawCircle(point);                  

                Intent proximityIntent = new Intent("in.wptrafficanalyzer.activity.proximity");                 

                pendingIntent = PendingIntent.getActivity(getBaseContext(), 0,   proximityIntent,Intent.FLAG_ACTIVITY_NEW_TASK);                    


                locationManager.addProximityAlert(point.latitude, point.longitude, 20, -1, pendingIntent);  

                SharedPreferences.Editor editor = sharedPreferences.edit();

                editor.putString("lat", Double.toString(point.latitude));

                editor.putString("lng", Double.toString(point.longitude));

                editor.putString("zoom", Float.toString(googleMap.getCameraPosition().zoom));               

                editor.commit();                

                Toast.makeText(getBaseContext(), "Proximity Alert is added", Toast.LENGTH_SHORT).show();                    

            }
        });    

        googleMap.setOnMapLongClickListener(new OnMapLongClickListener() {              
            @Override
            public void onMapLongClick(LatLng point) {
                Intent proximityIntent = new Intent("in.wptrafficanalyzer.activity.proximity");

                pendingIntent = PendingIntent.getActivity(getBaseContext(), 0, proximityIntent,Intent.FLAG_ACTIVITY_NEW_TASK);

                locationManager.removeProximityAlert(pendingIntent);

                googleMap.clear();

                SharedPreferences.Editor editor = sharedPreferences.edit();

                editor.clear();

                editor.commit();

                Toast.makeText(getBaseContext(), "Proximity Alert is removed", Toast.LENGTH_LONG).show();
            }
        });           
    }   
}   

private void drawMarker(LatLng point){
    MarkerOptions markerOptions = new MarkerOptions();                  

    markerOptions.position(point);

    googleMap.addMarker(markerOptions);

}


private void drawCircle(LatLng point){

    CircleOptions circleOptions = new CircleOptions();

    circleOptions.center(point);

    circleOptions.radius(20);

    circleOptions.strokeColor(Color.BLACK);

    circleOptions.fillColor(0x30ff0000);

    circleOptions.strokeWidth(2);

    googleMap.addCircle(circleOptions);

}   

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}
您导入FragmentActivity,但不使用它。活动不是来自v4或v7支持库。这应该起作用:

public class MainActivity extends FragmentActivity {

你在支持库上登广告了吗?当我尝试扩展ActionBarActivity时,android.app.Activity没有这样的方法。出现以下错误ActionBarActivity无法解析为类型您是否在xml中使用SupportMapFragment?