【问题标题】:update ui from another thread in android从android中的另一个线程更新ui
【发布时间】:2011-08-10 04:03:20
【问题描述】:

我想在 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;
        }
    }
}

【问题讨论】:

  • @ania 崩溃了,因为你不能用另一个线程来改变 UI。如果你想改变 UI 中的某些东西,你应该使用它自己的线程意味着 UI 线程。

标签: java android multithreading


【解决方案1】:

您不能从主线程以外的线程调用 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;
        }
    }
}

【讨论】:

    【解决方案2】:

    如下尝试

    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();
            registerReceiver(onBroadcast,new IntentFilter("abcd"));
    
        }
    
        String mySTR = "";
        TextView tv ;
    
        public void event(String absolutePath,String path,int event)
        {
            mySTR = absolutePath+path+"\t"+event;
                tv.setText(mySTR);  // program crash here!
        }
        @Override
        protected void onDestroy() {
    
            super.onDestroy();
            unregisterReceiver(onBroadcast);
        }
    
          private final BroadcastReceiver onBroadcast = new BroadcastReceiver() {
    
    
            @Override
            public void onReceive(Context ctxt, Intent i) {
                // do stuff to the UI
    
                event(absolutePath, path, event);
            }
        };
    }
    
    
    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)
            {
                 context.sendBroadcast(new Intent("abcd"));
                //fileobserveractivity.event(absolutePath, path, event);
            }
            else
            {
                return;
            }
        }
    }
    

    【讨论】:

    • 上下文是什么? 9 行结束?
    • thansk...它成功了。with ContextWrapper context = new ContextWrapper(fileobserveractivity);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-30
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    相关资源
    最近更新 更多