Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java GPS应用程序崩溃_Java_Android_Eclipse_Gps - Fatal编程技术网

Java GPS应用程序崩溃

Java GPS应用程序崩溃,java,android,eclipse,gps,Java,Android,Eclipse,Gps,我必须写一个应用程序,它可以做几件事,包括在输入正确的用户名和密码时记录用户的GPS位置。我只想要那个时刻的经纬度,不需要反复检查或任何类似的东西。我想我找到了与我所寻找的相似的东西: 然而,eclipse不喜欢这个上下文,因为我还没有初始化它。下面是代码的整个部分: Context context = getBaseContext(); LocationManager locMan = (LocationManager)context.getSystemService(Context.LOC

我必须写一个应用程序,它可以做几件事,包括在输入正确的用户名和密码时记录用户的GPS位置。我只想要那个时刻的经纬度,不需要反复检查或任何类似的东西。我想我找到了与我所寻找的相似的东西:

然而,eclipse不喜欢这个上下文,因为我还没有初始化它。下面是代码的整个部分:

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"/>
这是LogCat错误条目,它有点超出我的理解范围:

04-28 18:00:39.346:E/AndroidRuntime(32548):致命异常:主 04-28 18:00:39.346:E/AndroidRuntime(32548):过程: com.rps.personaldata,PID:3254804-2818:00:39.346: E/AndroidRuntime(32548):java.lang.NullPointerException 04-28 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.Invokenactive(本机方法)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(本机方法)


我是android新手,我只是想弄明白这一切。感谢您的帮助。

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

更改此选项:

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

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

LocationManager locMan=(LocationManager)getSystemService(Context.LOCATION\u服务)

locMan.getLastKnownLocation(LocationManager.GPS\U提供商)
返回的是
null
,这就是为什么会出现NullPointerException。任何
上下文
都应该用于检索
LocationManager

getLastKnownLocation(LocationManager.GPS\u PROVIDER)
如果最近没有使用GPS(特别是上次重新启动后),则可能返回
null

如果您的应用程序可以接受稍微不太准确的位置(例如,信元塔级别),则更好的方法是通过所有LocationProviders进行循环,并找到一个非空位置:

public static Location getLocation2(Context cxt) {
    LocationManager mgr = (LocationManager) cxt.getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = mgr.getProviders(true);
    Location last = null;
    for (Iterator<String> i = providers.iterator(); i.hasNext(); ) {
        Location loc = mgr.getLastKnownLocation(i.next());
        // If this provider has a last location, and either:
        // 1. We don't have a last location,
        // 2. Our last location is older than this location.
        if (loc != null && (last == null || loc.getTime() > last.getTime())) {
            last = loc;
        }
    }
    return last;
}
公共静态位置getLocation2(上下文cxt){
LocationManager Manager=(LocationManager)cxt.getSystemService(Context.LOCATION\u服务);
List providers=mgr.getProviders(true);
位置last=null;
for(迭代器i=providers.Iterator();i.hasNext();){
Location loc=mgr.getlastnownlocation(i.next());
//如果此提供程序有最后一个位置,并且:
//1.我们没有最后的位置,
//2.我们上一个位置比这个位置旧。
如果(loc!=null&&(last==null | | loc.getTime()>last.getTime()){
最后=loc;
}
}
最后返回;
}
这将使用提供您最近已知的GPS位置(如果可用),如果不可用,您可能会获得网络提供商的位置

请注意,在极少数情况下(在普通Android设备上),所有提供商都可能返回
null
。在这个场景中,您需要注册一个新位置,然后等待提供一个新位置。有关完整解决方案,请参阅

相关-有关考虑每个提供者每个位置准确性的代码,请参阅


最后一个音符——你可能想考虑使用新的(ER),这是谷歌游戏服务的一部分,在我的经验中,最好总是提供最后一个已知的位置。有关使用融合位置提供程序获取最后一个已知位置的示例代码,请参阅。

我还尝试使用getApplicationContext()初始化上下文。它似乎也在同一行崩溃。正如堆栈跟踪所说,问题在于com.rps.personaldata.MainActivity$1.onClick(MainActivity.java:58)。那是哪条线?第58行是这条线:双纬度=loc.getLatitude();显然,
loc
为空。文件解释了为什么会发生这种情况。不幸的是,这不起作用。它坠毁在同一个地方,在第58行,我称之为Double latitude=loc.getLatitude();。