gpt4 book ai didi

android - 当条件设置为高精度时应用程序崩溃

转载 作者:太空狗 更新时间:2023-10-29 15:35:54 26 4
gpt4 key购买 nike

我正在努力获得尽可能高的定位精度,这是我从 GPS 提供商那里了解到的。我只需要获取一次位置,所以我不关心对电池生命周期的影响。在我的功能代码中,位置似乎在完全不准确(1+km)到非常准确(10m 以内)之间交替。在我的应用程序的整个测试过程中,我在手机上启用/禁用位置、打开/关闭 wifi、打开/关闭数据。这只是为了实验,看看每个因素如何影响准确性和应用程序。

以下代码是我在位置 Activity 中的 onCreate 方法:

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

mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map_fragment))
.getMap();
mMap.setMyLocationEnabled(true);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();

//criteria.setAccuracy(Criteria.ACCURACY_HIGH);

provider = locationManager.getBestProvider(criteria, true);

mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();

// Create the LocationRequest object
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(2 * 1000) // 10 seconds, in milliseconds
.setFastestInterval(1 * 1000); // 1 second, in milliseconds
}

但是,当我添加 criteria.setAccuracy(Criteria.ACCURACY_HIGH); 行时,我的应用程序崩溃了。

这是怎么回事?

编辑:这是 logcat 错误:

E/AndroidRuntime: FATAL EXCEPTION: main    
E/AndroidRuntime: Process: com.locationget, PID: 16663
E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.locationget/com.locationget.MapActivity}: java.lang.IllegalArgumentException: accuracy=3
E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271
E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:144)
E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5146)
E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime: Caused by: java.lang.IllegalArgumentException: accuracy=3
E/AndroidRuntime: at android.location.Criteria.setAccuracy(Criteria.java:223)
E/AndroidRuntime: at com.getlocation.MapActivity.onCreate(MapActivity.java:47)
E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5231)
E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271
E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:144) 
E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5146) 
E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method) 
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515) 
E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566) 
E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method) 

我是 android 的新手,不太确定相关部分是什么。

最佳答案

这一行才是真正的错误:

E/AndroidRuntime: java.lang.RuntimeException: 无法启动 Activity ComponentInfo{com.locationget/com.locationget.MapActivity}:java.lang.IllegalArgumentException: accuracy=3

这是我经过一番搜索后发现的:

The android Criteria has two separate groups of accuracy constants. ACCURACY_FINE and ACCURACY_COARSE are for general location, while ACCURACY_LOW, ACCURACY_MEDIUM, and ACCURACY_HIGH "may be used for horizontal, altitude, speed or bearing accuracy."

所以在你的情况下,不要使用 ACCURACY_HIGH 否则你会得到一个错误。请改用 ACCURACY_COARSEACCURACY_FINE

关于android - 当条件设置为高精度时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33771390/

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