从Android的另一个线程更新界面 [英] update ui from another thread in android

查看:83
本文介绍了从Android的另一个线程更新界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变用户界面在Android中。

我的主类进行第二次类,那么第二类呼叫主类主class.method的方法应该更新运行时UI,但程序崩溃。

我应该怎么办?

我的主类:

 公共类FileObserverActivity扩展活动
{
    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        电视=(的TextView)findViewById(R.id.textView1);
        tv.setText(新的世界);
        MyFileObserver myFileObserver =新MyFileObserver(/ SD卡/,这一点);
        myFileObserver.startWatching();
    }    字符串myStr的=;
    TextView的电视;    公共无效事件(字符串absolutePath,字符串路径,INT事件)
    {
        myStr的= absolutePath +路径+\\ t+事件;
            tv.setText(myStr的); //程序崩溃在这里!
    }
}

和我的第二类:

 公共类MyFileObserver扩展FileObserver
{
    公共字符串absolutePath;
    FileObserverActivity fileobserveractivity;    公共MyFileObserver(字符串路径,FileObserverActivity FOA)
    {
        超(路径,FileObserver.ALL_EVENTS);
        absolutePath =路径;
        fileobserveractivity = FOA;
    }    @覆盖
    公共无效的onEvent(INT事件,字符串路径)
    {
        如果(路径== NULL)
        {
            返回;
        }
        否则,如果(事件!= 0)
        {
            fileobserveractivity.event(absolutePath,路径,事件);
        }
        其他
        {
            返回;
        }
    }
}


解决方案

您不能调用从非主线程其他线程的用户界面的方法。您应该使用<一个href=\"http://developer.android.com/reference/android/app/Activity.html#runOnUiThread%28java.lang.Runnable%29\">Activity#runOnUiThread()方法。

 公共类FileObserverActivity扩展活动
{
    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        电视=(的TextView)findViewById(R.id.textView1);
        tv.setText(新的世界);
        MyFileObserver myFileObserver =新MyFileObserver(/ SD卡/,这一点);
        myFileObserver.startWatching();
    }    字符串myStr的=;
    TextView的电视;    公共无效事件(字符串absolutePath,字符串路径,INT事件)
    {
        runOnUiThread(动作);
    }    私人Runnable的行动=新的Runnable(){
        @覆盖
        公共无效的run(){
            myStr的= absolutePath +路径+\\ t+事件;
            tv.setText(myStr的);
        }
    };
}公共类MyFileObserver扩展FileObserver
{
    公共字符串absolutePath;
    FileObserverActivity fileobserveractivity;    公共MyFileObserver(字符串路径,FileObserverActivity FOA)
    {
        超(路径,FileObserver.ALL_EVENTS);
        absolutePath =路径;
        fileobserveractivity = FOA;
    }    @覆盖
    公共无效的onEvent(INT事件,字符串路径)
    {
        如果(路径== NULL)
        {
            返回;
        }
        否则,如果(事件!= 0)
        {
            fileobserveractivity.event(absolutePath,路径,事件);
        }
        其他
        {
            返回;
        }
    }
}

i want to change UI in android.

my main class make a second class then second class call a method of main class.method in main class should update UI but program crash in runtime.

what should i do?

my main class :

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!
    }
}

and my second class :

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;
        }
    }
}

解决方案

You can't call UI methods from threads other than the main thread. You should use Activity#runOnUiThread() method.

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;
        }
    }
}

这篇关于从Android的另一个线程更新界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆