Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.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 类的runOnUiThread未定义_Java_Android_Multithreading_Dialog - Fatal编程技术网

Java 类的runOnUiThread未定义

Java 类的runOnUiThread未定义,java,android,multithreading,dialog,Java,Android,Multithreading,Dialog,我试图从我的后台线程在UI线程上抛出并警告对话框,但是我遇到了runOnUiThread未定义的问题。我尝试了FindLocation.this.runOnUiThread和runOnUiThread,但它们似乎都抛出了相同的错误方法runOnUiThread(new Runnable(){})对于类型new LocationListener(){}(或…类型FindLocation)是未定义的。你知道为什么吗?下面是我的FindLocation.java类的一个片段。这就是我的主要活动 pub

我试图从我的后台线程在UI线程上抛出并警告对话框,但是我遇到了runOnUiThread未定义的问题。我尝试了
FindLocation.this.runOnUiThread
和runOnUiThread,但它们似乎都抛出了相同的错误
方法runOnUiThread(new Runnable(){})对于类型new LocationListener(){}
(或
…类型FindLocation
)是未定义的。你知道为什么吗?下面是我的FindLocation.java类的一个片段。这就是我的主要活动

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) {
                           }
                        });
                    }
                });
公共类FindLocation扩展线程{
公共语言文字;
public boolean AlertNotice=false;
私人场所经理;
私有位置侦听器;
上下文ctx;
公共字符串用户标识;
公共查找位置(上下文ctx){
this.ctx=ctx;
}
公共void开始(字符串userId){
this.userId=userId;
super.start();
}
@凌驾
公开募捐{
Looper.prepare();
最终字符串usr=userId;
//获取LocationManager的引用
locManager=(LocationManager)ctx.getSystemService(Context.LOCATION\u服务);
//选中以接收来自该职位的更新
locListener=新的LocationListener(){
位置更改后的公共无效(位置loc){
String lat=String.valueOf(loc.getLatitude());
String lon=String.valueOf(loc.getLongitude());
双纬度=loc.getLatitude();
双经度=loc.getLongitude();
如果(纬度>=39.15296和经度>=-86.547546和纬度
runOnUIThread()
是属于
活动的方法,则无法从线程调用它

因此,不要使用上下文,而是在其构造函数中获取活动实例,并使用它调用它

activity.runOnUIThread();
由于
runOnUIThread()
Activity
的方法,所以可以在构造函数中传递对调用Activity的引用

...
Context ctx;
Activity act;
public String userId;
...

public FindLocation(Context ctx, Activity act) {
    this.ctx = ctx;
    this.act = act;
}
并使用类似的
runOnUIThread()

act.runOnUiThread(new Runnable() {...});

但是,我认为这是不安全的,您需要采取预防措施,确保在调用
runOnUiThread
时您的活动仍然存在。我发现了一种更简洁、更模块化的方法。为此,您需要定义一个
应用程序上下文
。一旦这样做,您就可以调用
runOnUiThread
m任何一个类库都没有对
活动的引用

从类库中的任意位置调用:

Handler handler = new Handler(Application.Context.MainLooper);
handler.Post(() => doStuff());
请记住,这是用C编写的,因为我使用MonoDroid,但我相信它与Java非常相似。 有关如何创建ApplicationContext,请参见

无需为获取活动创建构造函数。

只需将上下文类型转换为Activity类

((Activity)context).runOnUiThread(new Runnable()
    {
        public void run()
        { 
             Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();
        }
    });

您可以扩展类以包含活动 像这样

public class YourClass extends Activity {}
这将使“RunUnuithRead”可用

还要记住导入活动

import android.app.Activity;

runOnUiThread和TimerTask并不是制造计时器的最佳方法。我的两分钱可以避免它们,因为还有其他更有效的方法可以帮助你达到同样的目的。 TimerTask是最糟糕的类之一。非常简单的人喜欢使用它。非常简单。

我昨天在处理Java套接字时也遇到过这种情况。 1.将活动传递给FindLocation构造函数
活动;
公共场所(活动){
这个。活动=活动;
}
然后使用活动对象调用runOnUIThread()

this.activity.runOnUiThread(新的Runnable(){
...
});

这是因为
runOnUiThread
android.app.Activity
的方法,而不是
java.lang.Thread
Shoot。有没有实现类似功能的方法?@MartinSykes nope,@MartinSykes请删除该评论。。这是误导性的,因为上下文无法访问runOnUiThread()如果ctx是一个活动的实例,它将有rununuithread。比我之前的评论更清楚。好的,我可以在eclipse中实现它而不会出错,但是当我运行它时,我在
act.rununuithread(new Runnable(){…});
行上得到一个NullPointerException错误。为什么会发生这种情况?这可能与我的活套有关吗?尝试通过在
act.runOnUiThread()之前添加
Log.d(“Test”+“”+act);
来找出
null
-如果它不是
act
,那么问题就出在您的
可运行的
中的某个地方,我想我刚刚找到了它。当我从我的主要活动启动线程时,我使用以下语句:
newfindlocation(getBaseContext(),null)。start(usr\u id1);
我不确定传递什么而不是null。有什么想法吗?我尝试了
this
MainActivity
但都不起作用。是的,问题就在这里。如果你从activity本身调用它,它应该可以与
this
一起工作。如果调用来自内部类,
MainActivity。这个
应该可以工作。要获取activity、 你可以使用
getActivity().runOnUiThread(…
。在我的例子中,它引发了异常:java.lang.ClassCastException:android.app.ContextImpl无法强制转换为android.app.Activity。我找到了这个问题的解决方案:。还有什么方法可以成功地使用你的方式吗?@Nepster
import android.app.Activity;