gpt4 book ai didi

谷歌地图 API v2 的 java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-11-29 21:21:03 25 4
gpt4 key购买 nike

多年来我一直在尝试解决这个问题,但我找不到问题出在哪里。我只在某些运行 Android 2.2 的设备上遇到问题。我在 Android 4.1.2 上测试过,它运行良好。

我得到的错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.coldice.plotfinder/com.coldice.plotfinder.MapFragment}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2668)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2684)
at android.app.ActivityThread.access$2300(ActivityThread.java:126)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4632)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.coldice.plotfinder.MapFragment.onCreate(MapFragment.java:67)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2632)
... 11 more

所以回顾我的类 MapFragment 第 67 行是

 googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

我知道 googlemap API v2 需要 API 级别 12 或更高级别 as noted here , 但我用的是 lower 所以我调用

getSupportFragmentManager();

MapFragment类代码如下:

public class MapFragment extends SherlockFragmentActivity {
private static final String TAG = "MapFragment";
private GoogleMap googleMap; // Google map
private int mapType = GoogleMap.MAP_TYPE_SATELLITE;

private Polyline polyline;; // Drawing the area of the land by using the polygon
private boolean isDrawn=false;
static String fileToBeRead =null; // The file name to be read

ArrayList<Cooridnates> cooridnatesList;
// Image utility saving
ImageSaveUtil imageUtil = new ImageSaveUtil();

@Override
public void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_Sherlock);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);

// Different colors for each marker
float[] markerColours = {BitmapDescriptorFactory.HUE_AZURE,BitmapDescriptorFactory.HUE_BLUE,BitmapDescriptorFactory.HUE_CYAN,BitmapDescriptorFactory.HUE_GREEN,
BitmapDescriptorFactory.HUE_MAGENTA,BitmapDescriptorFactory.HUE_ORANGE,BitmapDescriptorFactory.HUE_RED,BitmapDescriptorFactory.HUE_ROSE,
BitmapDescriptorFactory.HUE_VIOLET,BitmapDescriptorFactory.HUE_YELLOW};

// The code I am getting java.lang.NullPointerException
FragmentManager fragmentManager = getSupportFragmentManager();
SupportMapFragment mapFragment = (SupportMapFragment)fragmentManager.findFragmentById(R.id.map);
googleMap = mapFragment.getMap();
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

// If there is a file to be read, then read the coordinates
if(fileToBeRead!=null)
{
readData(fileToBeRead);
fileToBeRead =null;
}

// Getting the coordinates List
cooridnatesList = MainActivity.getList();
if(cooridnatesList!=null)
{
// Adding a marker from each point
for(int i=0;i<cooridnatesList.size();i++)
{
LatLng point = new LatLng(cooridnatesList.get(i).getLat(),cooridnatesList.get(i).getLon());
googleMap.addMarker(new MarkerOptions()
.position(point)
.title("My Land")
.snippet("Point: "+cooridnatesList.get(i).getLat()+","+cooridnatesList.get(i).getLon())
.icon(BitmapDescriptorFactory.defaultMarker(markerColours[i%10])));

}

googleMap.getUiSettings().setCompassEnabled(true); // Setting the compass enabled in the map
googleMap.getUiSettings().setZoomControlsEnabled(true); // Zooming is enabled
googleMap.getUiSettings().setMyLocationButtonEnabled(true); // My location button is enabled


// Zooming the camera from the first point entered
LatLng cameraLatLng = new LatLng(cooridnatesList.get(0).getLat(),cooridnatesList.get(0).getLon());
float cameraZoom = 10;

if(savedInstanceState != null){
mapType = savedInstanceState.getInt("map_type", GoogleMap.MAP_TYPE_SATELLITE);

double savedLat = savedInstanceState.getDouble("lat");
double savedLng = savedInstanceState.getDouble("lng");
cameraLatLng = new LatLng(savedLat, savedLng);

cameraZoom = savedInstanceState.getFloat("zoom",18);
}

// Setting the map type such as satellites or street view
googleMap.setMapType(mapType);
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(cameraLatLng, cameraZoom));


//Displaying the land area
final TextView landInformation = (TextView)findViewById(R.id.textViewLandInformation);
double area = calculateArea(cooridnatesList);
landInformation.setText(getString(R.string.land_area)+"\n"+Math.round(area)+" SQ.M");

}

}

最佳答案

尝试将所有引用您的 GoogleMap 的代码移动到 onStart() 或 onResume()。直到 fragment 通过 onCreateView ( link ) 之后, map fragment 中的 map 才会被实例化。此外,您需要检查您的 googleMap 是否为 null,因为如果未安装 google play 服务,或者由于其他原因 map 不可用,它将为 null。

关于谷歌地图 API v2 的 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20728069/

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