我有一个非常普通的服务,可以在我的LG设备上执行应用程序时无缝运行。然而,当我在Nexus 5的Eclipse模拟器上执行此操作时,我遇到了崩溃,让我怀疑这种行为也可能出现在与我不同的真实设备上。这是代码:
Intent startBackgroundLocationIntent= new Intent(this, BackgroundLocationService.class);
startService(startBackgroundLocationIntent);
和崩溃日志:
08-13 14:18:10.731:E / AndroidRuntime(6345):致命异常:主要 08-13 14:18:10.731:E / AndroidRuntime(6345):流程: com.example.taxiprofessional,PID:6345 08-13 14:18:10.731: E / AndroidRuntime(6345):java.lang.RuntimeException:无法启动 服务 com.example.taxiprofessional.BackgroundLocationService@b20e39b0 with Intent {cmp = com.example.taxiprofessional / .BackgroundLocationService }:java.lang.NullPointerException 08-13 14:18:10.731: E / AndroidRuntime(6345):at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2719) 08-13 14:18:10.731:E / AndroidRuntime(6345):at android.app.ActivityThread.access $ 2100(ActivityThread.java:135)08-13 14:18:10.731:E / AndroidRuntime(6345):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1293) 08-13 14:18:10.731:E / AndroidRuntime(6345):at android.os.Handler.dispatchMessage(Handler.java:102)08-13 14:18:10.731:E / AndroidRuntime(6345):at android.os.Looper.loop(Looper.java:136)08-13 14:18:10.731: E / AndroidRuntime(6345):at android.app.ActivityThread.main(ActivityThread.java:5017)08-13 14:18:10.731:E / AndroidRuntime(6345):at java.lang.reflect.Method.invokeNative(Native Method)08-13 14:18:10.731:E / AndroidRuntime(6345):at java.lang.reflect.Method.invoke(Method.java:515)08-13 14:18:10.731: E / AndroidRuntime(6345):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 08-13 14:18:10.731:E / AndroidRuntime(6345):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)08-13 14:18:10.731:E / AndroidRuntime(6345):at dalvik.system.NativeStart.main(Native Method)08-13 14:18:10.731: E / AndroidRuntime(6345):引起:java.lang.NullPointerException 08-13 14:18:10.731:E / AndroidRuntime(6345):at com.example.taxiprofessional.BackgroundLocationService.uploadCoordinatesForLocation(BackgroundLocationService.java:100) 08-13 14:18:10.731:E / AndroidRuntime(6345):at com.example.taxiprofessional.BackgroundLocationService.onStartCommand(BackgroundLocationService.java:138) 08-13 14:18:10.731:E / AndroidRuntime(6345):at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2702) 08-13 14:18:10.731:E / AndroidRuntime(6345):... 10 more
答案 0 :(得分:0)
读取该堆栈跟踪,看起来崩溃的代码是您的上传方法:
E/AndroidRuntime(6345): Caused by: java.lang.NullPointerException 08-13 14:18:10.731: E/AndroidRuntime(6345): at com.example.taxiprofessional.BackgroundLocationService.uploadCoordinatesForLocation(BackgroundLocationService.java:100)
只是在这里推测,但你验证过你是否检查了你试图上传的坐标?除非您正确配置模拟位置,否则您的模拟器将没有位置。
答案 1 :(得分:0)
我通过在调用函数之前检查变量last是否为null并通过请求最佳提供者来修复整个问题;这是我希望像我这样的初学者使用的代码片段:
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String provider = lm.getBestProvider(criteria, true);
if (lm!=null) {
Location last=lm.getLastKnownLocation(provider);
if (last!=null) uploadCoordinatesForLocation(last);
lm.requestLocationUpdates(provider, 0, 0, locationListener);
}
谢谢大家。