Android GPS一次创建问题

Android GPS一次创建问题,android,android-activity,gps,Android,Android Activity,Gps,我被一个我搞不清楚的问题缠住了。我尝试过很多方法,但没有一种能让我明白为什么它们不起作用 问题 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android

我被一个我搞不清楚的问题缠住了。我尝试过很多方法,但没有一种能让我明白为什么它们不起作用

问题

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<EditText
android:id="@+id/lat"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="My text edit this." >
    <requestFocus />
</EditText>

<EditText
android:id="@+id/lng"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="My text edit this."
 />
每次我想运行应用程序时,它都会崩溃。在43号线,上面写着//Crash在这里

我想做的是,我有两个文本视图,输入十进制

Sry我复制了错误的xml,但仍然存在相同的问题

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<EditText
android:id="@+id/lat"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="My text edit this." >
    <requestFocus />
</EditText>

<EditText
android:id="@+id/lng"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="My text edit this."
 />
日志

  02-11 13:59:16.499: D/AndroidRuntime(10702): Shutting down VM
  02-11 13:59:16.499: W/dalvikvm(10702): threadid=1: thread exiting with uncaught             exception (group=0x41f592a0)
  02-11 13:59:16.504: E/AndroidRuntime(10702): FATAL EXCEPTION: main
  02-11 13:59:16.504: E/AndroidRuntime(10702): java.lang.RuntimeException: Unable to       start activity ComponentInfo{com.androidhive.jsonparsing/com.androidhive.jsonparsing.Starter}: java.lang.NullPointerException
  02-11 13:59:16.504: E/AndroidRuntime(10702):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
  02-11 13:59:16.504: E/AndroidRuntime(10702):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
  02-11 13:59:16.504: E/AndroidRuntime(10702):  at android.app.ActivityThread.access$600(ActivityThread.java:140)
  02-11 13:59:16.504: E/AndroidRuntime(10702):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
  02-11 13:59:16.504: E/AndroidRuntime(10702):  at android.os.Handler.dispatchMessage(Handler.java:99)
  02-11 13:59:16.504: E/AndroidRuntime(10702):  at android.os.Looper.loop(Looper.java:137)
  02-11 13:59:16.504: E/AndroidRuntime(10702):  at android.app.ActivityThread.main(ActivityThread.java:4898)
  02-11 13:59:16.504: E/AndroidRuntime(10702):  at java.lang.reflect.Method.invokeNative(Native Method)
  02-11 13:59:16.504: E/AndroidRuntime(10702):  at java.lang.reflect.Method.invoke(Method.java:511)
  02-11 13:59:16.504: E/AndroidRuntime(10702):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
  02-11 13:59:16.504: E/AndroidRuntime(10702):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
  02-11 13:59:16.504: E/AndroidRuntime(10702):  at dalvik.system.NativeStart.main(Native Method)
  02-11 13:59:16.504: E/AndroidRuntime(10702): Caused by: java.lang.NullPointerException
  02-11 13:59:16.504: E/AndroidRuntime(10702):  at com.androidhive.jsonparsing.Starter.onCreate(Starter.java:44)
  02-11 13:59:16.504: E/AndroidRuntime(10702):  at android.app.Activity.performCreate(Activity.java:5206)
  02-11 13:59:16.504: E/AndroidRuntime(10702):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
  02-11 13:59:16.504: E/AndroidRuntime(10702):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
  02-11 13:59:16.504: E/AndroidRuntime(10702):  ... 11 more
这是需要的


当应用程序启动时,我想让它获得纬度和经度,并将其放在文本视图中,显示当前位置的纬度和经度值

从stacktrace中,我会说LatText为null,因为您没有使用正确的id

尝试使用
LatText=(EditText)findviewbyd(R.id.lat)


旧答案

我认为你在这里做的事情是倒退的:

  • LocationManager.requestLocationUpdates()
    可能需要很长时间,应该在后台线程中完成
  • 启动/完成活动或从后台线程修改UI是不安全的。您应该使用UI线程来实现这一点
  • 您可以使用后台线程请求位置,并使用
    runOnUiThread
    更新文本视图或启动新活动

    重要提示:确保停止活动的onPause方法中的线程

    更换

    LatText.setText(Double.toString(currentLat));
    LongText.setText(Double.toString(currentLong));
    


    这个答案是错误的。你可以从主线程调用requestLocationUpdates,它根本不阻止任何东西,因为当它得到一个位置时,它会触发回调onLocationChanged(),好的,嗯,任何人都可以为我找到这个问题的解决方案吗?@NickT似乎你是对的,不知道为什么我认为你应该从后台线程调用它…你能发布你的错误跟踪吗?
    LatText.setText(String.valueOf(currentLat));
    LongText.setText(String.valueOf(currentLong));