“无法从事件线程调用 getLocation() 方法”
我正在开发一个应用程序,试图获取当前位置。但我收到错误“无法从事件线程调用 getLocation() 方法”,即使我创建了一个新线程。有人可以帮我吗?
这是我的代码
Thread t = new MyThread();
t.run();
,这是 MyThread 类的 run 函数:
try
{
Criteria cr = new Criteria();
cr.setHorizontalAccuracy(500);
LocationProvider lp = LocationProvider.getInstance(cr);
Location l = lp.getLocation(60);
Coordinates c = l.getQualifiedCoordinates();
if (c != null)
{
double longitude = c.getLongitude();
double latitude = c.getLatitude();
Dialog.alert("latitude:"+latitude);
Dialog.alert("longitude:"+longitude);
}
}catch(Exception e){
Dialog.alert(e.getMessage());
}
I'm developing an application where am trying to get the current location. But am getting an error "getLocation() method cannot be called from event thread" even though I have created a new thread. Can anyone help me out?
Here is my code
Thread t = new MyThread();
t.run();
and here is the run function of MyThread class:
try
{
Criteria cr = new Criteria();
cr.setHorizontalAccuracy(500);
LocationProvider lp = LocationProvider.getInstance(cr);
Location l = lp.getLocation(60);
Coordinates c = l.getQualifiedCoordinates();
if (c != null)
{
double longitude = c.getLongitude();
double latitude = c.getLatitude();
Dialog.alert("latitude:"+latitude);
Dialog.alert("longitude:"+longitude);
}
}catch(Exception e){
Dialog.alert(e.getMessage());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能通过调用 run() 方法来启动线程。您可以通过调用 start() 方法来启动线程。
You don't start a thread by calling the run() method. You start a thread by calling the start() method.