GPS应用程序崩溃 [英] GPS Application crash

查看:206
本文介绍了GPS应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须编写一个可以进行多项操作的应用程序,包括在输入正确的用户名和密码时记录用户的GPS位置。我只想在那个确切时刻的纬度和经度,没有反复检查或任何这种性质。我想我发现了类似于我正在寻找的东西:

I have to write an application which does several things, including recording the GPS location of the user when a correct username and password is entered. I only want the latitude and longitude at that exact moment, no recurring checks or anything of that nature. I think I found something similar to what I was looking for:

当我想要android时,如何调用获取单个gps修复?

然而,由于我没有初始化它,eclipse并不喜欢上下文。所以这里是代码的整个部分:

However, eclipse didn't like the context since I hadn't initialized it yet. So here is that entire section of the code:

Context context = getBaseContext();
LocationManager locMan = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
Log.i(getClass().getSimpleName(),"Location Manager called");
Location loc = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.i(getClass().getSimpleName(),"Last known location received");
Double latitude = loc.getLatitude();
Log.i(getClass().getSimpleName(),"Latitude:"+latitude);
Double longitude = loc.getLongitude();
Log.i(getClass().getSimpleName(),"Longitude:"+longitude);
Toast.makeText(context, latitude+","+longitude, Toast.LENGTH_LONG).show();

对于清单中的权限:

<uses-permission android:name="com.foxcatch.code.insecure.LOCATION_FINDER" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

但是,当我尝试访问纬度和经度并将其分配给双精度时,这会崩溃:

However this crashes when I try to access the latitude and longitude and assign them to doubles:

Double latitude = loc.getLatitude();
Double longitude = loc.getLongitude();

这是LogCat错误条目,其中有一点我的头:

Here's the LogCat Error entry, its a little over my head:


04-28 18:00:39.346:E / AndroidRuntime(32548):FATAL EXCEPTION:main
04-28 18:00:39.346:E / AndroidRuntime (32548):进程:
com.rps.personaldata,PID:32548 04-28 18:00:39.346:
E / AndroidRuntime(32548):java.lang.NullPointerException 04-28
18:00:39.346:E / AndroidRuntime(32548):at
com.rps.personaldata.MainActivity $ 1.onClick(MainActivity.java:58)
04-28 18:00:39.346:E / AndroidRuntime(32548):at
android.view.View.performClick(View.java:4445)04-28 18:00:39.346:
E / AndroidRuntime(32548):at
android.view.View $ PerformClick.run(View.java:18429)04-28
18:00:39.346:E / AndroidRuntime(32548):at
android.os.Handler.handleCallback(Handler .java:733)04-28
18:00:39.346:E / AndroidRuntime(32548):at
android.os.Handler.dispatchMessage(Handler.java:95)04-28
18:00:39.346: E / AndroidRuntime(32548):
android.os.Looper.loop(Looper.java:136)04-28 18:00:39.346:
E / AndroidRuntime(32548):at
android.app.ActivityThread.main(ActivityThread.java:5081)04-28
18:00:39.346:E / AndroidRuntime(32548):at
java.lang.reflect.Method.invokeNative(本地方法)04-28
18:00:39.346:E / AndroidRuntime(32548):at
java.lang.reflect.Method.invoke(Method.java:515)04-28 18:00 :39.346:
E / AndroidRuntime(32548):
com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:781)
04-28 18:00: 39.346:E / AndroidRuntime(32548):
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)04-28
18:00:39.346:E / AndroidRuntime(32548 ):at
dalvik.system.NativeStart.main(Native Method)

04-28 18:00:39.346: E/AndroidRuntime(32548): FATAL EXCEPTION: main 04-28 18:00:39.346: E/AndroidRuntime(32548): Process: com.rps.personaldata, PID: 32548 04-28 18:00:39.346: E/AndroidRuntime(32548): java.lang.NullPointerException 04-28 18:00:39.346: E/AndroidRuntime(32548): at com.rps.personaldata.MainActivity$1.onClick(MainActivity.java:58) 04-28 18:00:39.346: E/AndroidRuntime(32548): at android.view.View.performClick(View.java:4445) 04-28 18:00:39.346: E/AndroidRuntime(32548): at android.view.View$PerformClick.run(View.java:18429) 04-28 18:00:39.346: E/AndroidRuntime(32548): at android.os.Handler.handleCallback(Handler.java:733) 04-28 18:00:39.346: E/AndroidRuntime(32548): at android.os.Handler.dispatchMessage(Handler.java:95) 04-28 18:00:39.346: E/AndroidRuntime(32548): at android.os.Looper.loop(Looper.java:136) 04-28 18:00:39.346: E/AndroidRuntime(32548): at android.app.ActivityThread.main(ActivityThread.java:5081) 04-28 18:00:39.346: E/AndroidRuntime(32548): at java.lang.reflect.Method.invokeNative(Native Method) 04-28 18:00:39.346: E/AndroidRuntime(32548): at java.lang.reflect.Method.invoke(Method.java:515) 04-28 18:00:39.346: E/AndroidRuntime(32548): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781) 04-28 18:00:39.346: E/AndroidRuntime(32548): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 04-28 18:00:39.346: E/AndroidRuntime(32548): at dalvik.system.NativeStart.main(Native Method)

我是Android的新手,而我只是想把所有这一切都算出来。感谢任何帮助。

I'm new to android, and I'm just trying to figure all of this out. Thanks for any help.

推荐答案

为什么要获取BaseContext ..只需直接调用 getSystemService

Why getting the BaseContext.. just simply call directly the getSystemService

更改此:

LocationManager locMan = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);

至:

LocationManager locMan =(LocationManager)getSystemService(Context.LOCATION_SERVICE);

LocationManager locMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

这篇关于GPS应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆