gpt4 book ai didi

java - E/AndroidRuntime : FATAL EXCEPTION: main at dalvik. system.NativeStart.main(Native Method) 崩溃

转载 作者:行者123 更新时间:2023-12-03 17:46:48 25 4
gpt4 key购买 nike

对于 Android使用 Android Studio 2.0这是我的代码

package com.innovapp.maptest;

import android.location.Location;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMyLocationEnabled(true);
Location myLocation = new Location(mMap.getMyLocation());

LatLng myLatLng = new LatLng(myLocation.getLatitude(),myLocation.getLongitude());
mMap.addMarker(new MarkerOptions().position(myLatLng).title("Your Location"+myLocation.getAccuracy()));
mMap.moveCamera(CameraUpdateFactory.newLatLng(myLatLng));
}
}

这是我的堆栈跟踪
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.innovapp.maptest, PID: 14690
java.lang.NullPointerException
at android.location.Location.set(Location.java:130)
at android.location.Location.<init>(Location.java:123)
at com.innovapp.maptest.MapsActivity.onMapReady(MapsActivity.java:32)
at com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source)
at com.google.android.gms.maps.internal.zzo$zza.onTransact(Unknown Source)
at android.os.Binder.transact(Binder.java:361)
at com.google.android.gms.maps.internal.be.a(SourceFile:82)
at com.google.maps.api.android.lib6.e.fb.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5052)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)

谁能帮我弄清楚为什么我会得到这个异常(exception)?

还有

据我了解, getMyLocation() 已弃用,那么我现在应该使用什么来保留代码的功能?

最佳答案

Map.getMyLocation() 可以返回 null,因此您应该在调用 getLatitude() 之前先检查它。另外,当您使用 map 时,我建议您实现 onLocationChangeListener 并在此方法中初始化您的 map

关于java - E/AndroidRuntime : FATAL EXCEPTION: main at dalvik. system.NativeStart.main(Native Method) 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35339235/

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