update ui from another thread in android

后端 未结 2 1693
夕颜
夕颜 2021-01-15 02:38

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 cra

相关标签:
2条回答
  • 2021-01-15 02:45

    Try as follows

    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;
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-15 02:51

    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;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题