【发布时间】:2012-04-17 00:51:54
【问题描述】:
我正在尝试从我的后台线程在 UI 线程上抛出和警告对话框,但我遇到了 runOnUiThread 未定义的问题。我尝试过FindLocation.this.runOnUiThread 和runOnUiThread,但似乎都抛出了相同的错误The method runOnUiThread(new Runnable(){}) is undefined for the type new LocationListener(){}(或...the type FindLocation)。任何想法为什么?这是我的 FindLocation.java 类的 sn-p。这是我的主要活动调用的。
public class FindLocation extends Thread {
public boolean inJurisdiction;
public boolean AlertNotice = false;
private LocationManager locManager;
private LocationListener locListener;
Context ctx;
public String userId;
public FindLocation(Context ctx) {
this.ctx = ctx;
}
public void start(String userId) {
this.userId = userId;
super.start();
}
@Override
public void run() {
Looper.prepare();
final String usr = userId;
//get a reference to the LocationManager
locManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
//checked to receive updates from the position
locListener = new LocationListener() {
public void onLocationChanged(Location loc) {
String lat = String.valueOf(loc.getLatitude());
String lon = String.valueOf(loc.getLongitude());
Double latitude = loc.getLatitude();
Double longitude = loc.getLongitude();
if (latitude >= 39.15296 && longitude >= -86.547546 && latitude <= 39.184901 && longitude <= -86.504288 || inJurisdiction != false) {
Log.i("Test", "Yes");
inJurisdiction = true;
FindLocation.this.runOnUiThread(new Runnable() { ///****error here****
public void run() {
AlertDialog.Builder alert = new AlertDialog.Builder(ctx);
alert.setTitle("Sent");
alert.setMessage("You will be contacted shortly.");
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
}
});
【问题讨论】:
-
那是因为
runOnUiThread是android.app.Activity的方法,而不是java.lang.Thread -
射击。有没有办法实现类似的东西?
-
@MartinSykes 不,it doesn't
-
@Martin Sykes 请删除该评论。它具有误导性,因为 Context 无法访问 runOnUIThread()
-
如果 ctx 是 Activity 的实例,它将具有 runOnUiThread。比我之前的评论更清楚。
标签: java android multithreading dialog