您不能从主线程以外的线程调用UI方法。您应该使用Activity#runOnUiThread()方法。
public class FileObserverActivity extends Activity { @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条)