gpt4 book ai didi

java - MainActivity 类型的 getSupportFragmentManager() 方法未定义 : android

转载 作者:行者123 更新时间:2023-12-01 07:05:11 24 4
gpt4 key购买 nike

我收到此错误: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,但没有使用它。 Activity 既不是来自 v4,也不是来自 v7 支持库。这应该有效:

public class MainActivity extends FragmentActivity {

关于java - MainActivity 类型的 getSupportFragmentManager() 方法未定义 : android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27048301/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com