java - 从android中的另一个线程更新ui

标签 java android multithreading

我想在 android 中更改 UI。

我的主类生成第二个类,然后第二个类调用主类的方法。主类中的方法应该更新 UI 但程序在运行时崩溃。

我该怎么办?

我的主课:

public class FileObserverActivity extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tv = (TextView) findViewById(R.id.textView1);
        tv.setText("new world");
        MyFileObserver myFileObserver = new MyFileObserver("/sdcard/", this);
        myFileObserver.startWatching();
    }

    String mySTR = "";
    TextView tv ;

    public void event(String absolutePath,String path,int event)
    {
        mySTR = absolutePath+path+"\t"+event;
            tv.setText(mySTR);  // program crash here!
    }
}

和我的第二堂课:

public class MyFileObserver extends FileObserver 
{
    public String absolutePath;
    FileObserverActivity fileobserveractivity;

    public MyFileObserver(String path,FileObserverActivity foa) 
    {
        super(path, FileObserver.ALL_EVENTS);
        absolutePath = path;
        fileobserveractivity = foa;
    }

    @Override
    public void onEvent(int event, String path) 
    {
        if (path == null) 
        {
            return;
        }
        else if(event!=0)
        {
            fileobserveractivity.event(absolutePath, path, event);
        }
        else
        {
            return;
        }
    }
}

最佳答案

您不能从主线程以外的线程调用 UI 方法。你应该使用 Activity#runOnUiThread()方法。

public class FileObserverActivity extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tv = (TextView) findViewById(R.id.textView1);
        tv.setText("new world");
        MyFileObserver myFileObserver = new MyFileObserver("/sdcard/", this);
        myFileObserver.startWatching();
    }

    String mySTR = "";
    TextView tv ;

    public void event(String absolutePath,String path,int event)
    {
        runOnUiThread(action);
    }

    private Runnable action = new Runnable() {
        @Override
        public void run() {
            mySTR = absolutePath+path+"\t"+event;
            tv.setText(mySTR);
        }
    };
}

public class MyFileObserver extends FileObserver 
{
    public String absolutePath;
    FileObserverActivity fileobserveractivity;

    public MyFileObserver(String path,FileObserverActivity foa) 
    {
        super(path, FileObserver.ALL_EVENTS);
        absolutePath = path;
        fileobserveractivity = foa;
    }

    @Override
    public void onEvent(int event, String path) 
    {
        if (path == null) 
        {
            return;
        }
        else if(event!=0)
        {
            fileobserveractivity.event(absolutePath, path, event);
        }
        else
        {
            return;
        }
    }
}

关于java - 从android中的另一个线程更新ui,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7005756/

相关文章:

java - 防止 arraylist 中的重复条目

java - 通过 eclipse 启动 jsp 程序时 Tomcat 8 端口问题

带有 Jquery Mobile 的 Javascript 不适用于 Android 2.3 及更低版本

android - 在 Android TV 上将 ProgressBar 添加到 DetailsOverviewRow

c# - 检查多台机器上的开放端口

c++ - 线程安全标准 :list C++

java - HashMap 冲突会导致调整大小吗?

java - 获取异常 java.security.InvalidKeyException : Invalid AES key length: 29 bytes?

安卓 C++ : reading text file from assets using ndk

python - 在类初始化中每 X 秒执行一次函数