本文介绍了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:

How打电话得到一个单一的GPS定位当过我想使用Android?

不过,日食并不喜欢,因为我还没有初始化它尚未上下文。因此,这里的是,code的整个部分:

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:

18 04-28:00:39.346:E / AndroidRuntime(32548):致命异常:主要
  04-28 18:00:39.346:E / AndroidRuntime(32548):过程:
  com.rps.personaldata,PID:32548 04-28 18:00:39.346:
  E / AndroidRuntime(32548):04-28显示java.lang.NullPointerException
  18:00:39.346:E / AndroidRuntime(32548):在
  com.rps.personaldata.MainActivity $ 1.onClick(MainActivity.java:58)
  04-28 18:00:39.346:E / AndroidRuntime(32548):在
  android.view.View.performClick(View.java:4445)04-28 18:00:39.346:
  E / AndroidRuntime(32548):在
  android.view.View $ PerformClick.run(View.java:18429)04-28
  18:00:39.346:E / AndroidRuntime(32548):在
  android.os.Handler.handleCallback(Handler.java:733)04-28
  18:00:39.346:E / AndroidRuntime(32548):在
  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):在
  android.app.ActivityThread.main(ActivityThread.java:5081)04-28
  18:00:39.346:E / AndroidRuntime(32548):在
  java.lang.reflect.Method.invokeNative(本机方法)04-28
  18:00:39.346:E / AndroidRuntime(32548):在
  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):在
  dalvik.system.NativeStart.main(本机方法)

我是新来的机器人,而我只是想弄清楚这一切了。感谢您的帮助。

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应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-07 14:27