[Android的]如何将当前位置设置为一个位置对象上点击一个按钮? [英] [Android]How to set the current location to a location object on click of a button?
问题描述
我想在当前位置设置到对象上的一个按钮的点击。但我得到一个错误的ANR,当我点击按钮。这是我讲的按钮,其中destinationObj声明是全球性的类型所在地的onClick的方法。请帮助我。
公共无效的onClick(视图v){ 双纬度,经度;
纬度= destinationObj.getLatitude();
经度= destinationObj.getLongitude();
destinationObj.setLatitude(LAT);
destinationObj.setLongitude(LON);
}
和logcat的:
10月9日至29日:44:02.456:E / AndroidRuntime(14449):致命异常:主要
10月9日至29日:44:02.456:E / AndroidRuntime(14449):java.lang.IllegalStateException:无法执行活动的方法
10月9日至29日:44:02.456:E / AndroidRuntime(14449):在android.view.View $ 1.onClick(View.java:3599)
10月9日至29日:44:02.456:E / AndroidRuntime(14449):在android.view.View.performClick(View.java:4204)
10月9日至29日:44:02.456:E / AndroidRuntime(14449):在android.view.View $ PerformClick.run(View.java:17355)
10月9日至29日:44:02.456:E / AndroidRuntime(14449):在android.os.Handler.handleCallback(Handler.java:725)
10月9日至29日:44:02.456:E / AndroidRuntime(14449):在android.os.Handler.dispatchMessage(Handler.java:92)
10月9日至29日:44:02.456:E / AndroidRuntime(14449):在android.os.Looper.loop(Looper.java:137)
10月9日至29日:44:02.456:E / AndroidRuntime(14449):在android.app.ActivityThread.main(ActivityThread.java:5041)
10月9日至29日:44:02.456:E / AndroidRuntime(14449):在java.lang.reflect.Method.invokeNative(本机方法)
10月9日至29日:44:02.456:E / AndroidRuntime(14449):在java.lang.reflect.Method.invoke(Method.java:511)
10月9日至29日:44:02.456:E / AndroidRuntime(14449):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
10月9日至29日:44:02.456:E / AndroidRuntime(14449):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10月9日至29日:44:02.456:E / AndroidRuntime(14449):在dalvik.system.NativeStart.main(本机方法)
10月9日至29日:44:02.456:E / AndroidRuntime(14449):由:java.lang.reflect.InvocationTargetException
10月9日至29日:44:02.456:E / AndroidRuntime(14449):在java.lang.reflect.Method.invokeNative(本机方法)
10月9日至29日:44:02.456:E / AndroidRuntime(14449):在java.lang.reflect.Method.invoke(Method.java:511)
10月9日至29日:44:02.456:E / AndroidRuntime(14449):在android.view.View $ 1.onClick(View.java:3594)
10月9日至29日:44:02.456:E / AndroidRuntime(14449):... 11个
10月9日至29日:44:02.456:E / AndroidRuntime(14449):致:显示java.lang.NullPointerException
10月9日至29日:44:02.456:E / AndroidRuntime(14449):在com.example.user.MainActivity.onClick(MainActivity.java:130)
10月9日至29日:44:02.456:E / AndroidRuntime(14449):... 14个
10月9日至29日:44:04.088:I /流程(14449):发送信号。 PID:14449 SIG:9
块引用>解决方案destinationObj.setLatitude(destinationObj.getLatitude());这是没有意义的?
destinationObj是用于设置器和吸气剂都是相同的对象。左手边应该是一个TextView或任何你想告诉的当前位置。
如果您setLatitude就像实现的:
this.latitude = getLatitude();
在code将进入一个循环,不断设置纬度变量。
请尝试按照本指南:
http://www.androidhive.info/2012/07/ Android的GPS定位管理器教程/在引导做到以下几点:
@覆盖
公共无效onLocationChanged(地点){
如果((this.location =位置)== NULL)
返回;
}然后在你的GPSTracker实例,让我们称呼它,GPS,拨打:
gps.getLatitude();
gps.getLongitude();要获得从GPS最新接收的位置(通知将读取相同的值,直到GPS装置先后落户)
如果你真的需要一个Location对象,那么你可以实现一个getter自己:
公共类GPSTracker ...
...
公共场所getLastLocation(){
返回this.location;
}
}I am trying to set the current location to an object on the click of a button. But I am getting an ANR error, when I click the button. This is the onClick method of the button I am talking about, where destinationObj is declared global and is of type Location. Please help me out.
public void onClick(View v){ double lat, lon; lat=destinationObj.getLatitude(); lon=destinationObj.getLongitude(); destinationObj.setLatitude(lat); destinationObj.setLongitude(lon); }
and the logcat:
09-29 10:44:02.456: E/AndroidRuntime(14449): FATAL EXCEPTION: main 09-29 10:44:02.456: E/AndroidRuntime(14449): java.lang.IllegalStateException: Could not execute method of the activity 09-29 10:44:02.456: E/AndroidRuntime(14449): at android.view.View$1.onClick(View.java:3599) 09-29 10:44:02.456: E/AndroidRuntime(14449): at android.view.View.performClick(View.java:4204) 09-29 10:44:02.456: E/AndroidRuntime(14449): at android.view.View$PerformClick.run(View.java:17355) 09-29 10:44:02.456: E/AndroidRuntime(14449): at android.os.Handler.handleCallback(Handler.java:725) 09-29 10:44:02.456: E/AndroidRuntime(14449): at android.os.Handler.dispatchMessage(Handler.java:92) 09-29 10:44:02.456: E/AndroidRuntime(14449): at android.os.Looper.loop(Looper.java:137) 09-29 10:44:02.456: E/AndroidRuntime(14449): at android.app.ActivityThread.main(ActivityThread.java:5041) 09-29 10:44:02.456: E/AndroidRuntime(14449): at java.lang.reflect.Method.invokeNative(Native Method) 09-29 10:44:02.456: E/AndroidRuntime(14449): at java.lang.reflect.Method.invoke(Method.java:511) 09-29 10:44:02.456: E/AndroidRuntime(14449): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 09-29 10:44:02.456: E/AndroidRuntime(14449): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 09-29 10:44:02.456: E/AndroidRuntime(14449): at dalvik.system.NativeStart.main(Native Method) 09-29 10:44:02.456: E/AndroidRuntime(14449): Caused by: java.lang.reflect.InvocationTargetException 09-29 10:44:02.456: E/AndroidRuntime(14449): at java.lang.reflect.Method.invokeNative(Native Method) 09-29 10:44:02.456: E/AndroidRuntime(14449): at java.lang.reflect.Method.invoke(Method.java:511) 09-29 10:44:02.456: E/AndroidRuntime(14449): at android.view.View$1.onClick(View.java:3594) 09-29 10:44:02.456: E/AndroidRuntime(14449): ... 11 more 09-29 10:44:02.456: E/AndroidRuntime(14449): Caused by: java.lang.NullPointerException 09-29 10:44:02.456: E/AndroidRuntime(14449): at com.example.user.MainActivity.onClick(MainActivity.java:130) 09-29 10:44:02.456: E/AndroidRuntime(14449): ... 14 more 09-29 10:44:04.088: I/Process(14449): Sending signal. PID: 14449 SIG: 9
解决方案destinationObj.setLatitude(destinationObj.getLatitude()); this makes no sense?
destinationObj being the same object for both the setter and getter. The left hand side should be a TextView or whatever you want to tell the current location.
if your setLatitude is implemented like:
this.latitude = getLatitude();
The code will enter a loop, continuously setting the latitude variable.
Please try and follow this guide: http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/
In the guide do the following:
@Override public void onLocationChanged(Location location) { if ((this.location = location) == null) return; }
Then on your instance of GPSTracker, lets call it, gps, call:
gps.getLatitude(); gps.getLongitude();
To get the latest received location from the GPS (notice will read the same values until the gps unit has settled)
If you really need a Location object then you can implement a getter yourself:
public class GPSTracker ... ... public Location getLastLocation() { return this.location; } }
这篇关于[Android的]如何将当前位置设置为一个位置对象上点击一个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!