我收到此错误:The method getSupportFragmentManager() is undefined for the type MainActivity.
当我将语句更改为:
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;
}
}
答案 0 :(得分:8)
您导入FragmentActivity
,但不使用它。 Activity
不是来自v4,也不来自v7支持库。这应该有效:
public class MainActivity extends FragmentActivity {