这是我的 onCreate 的一部分,有时会导致异常:
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tilisting);
    _context = getApplicationContext();
    SDName = Environment.getExternalStorageDirectory();
    //listview = (ListView)findViewById(R.id.TIlistview);
    String TIdir = new File(SDName, "/TitaniumBackup/").toString();
    final ArrayList<String> apps = new ArrayList<String>();
    final StringBuffer done = new StringBuffer();
    Command command = new Command(0,"ls -a "+TIdir+"/*.properties") {
        @Override
        public void output(int arg0, String arg1) {
            synchronized(apps) {
                apps.add(arg1);
                if (!done.toString().equals("")) {
                    done.append("done");//oh no
                }
            }
        }
    };
    try {
        RootTools.getShell(true).add(command).waitForFinish();
        String attrLine = "";
        int ind;
        backups = new ArrayList<TIBackup>();
        synchronized(apps) {
            for (String app : apps) {
                try {
                    TIBackup bkup = new TIBackup(app);
                    FileInputStream fstream = new FileInputStream(app);
                    BufferedReader atts = new BufferedReader(new InputStreamReader(fstream));
                    while ((attrLine = atts.readLine()) != null) {
                        ind = attrLine.indexOf('=');
                        if (ind !=-1 && !attrLine.substring(0,1).equals("#"))
                        bkup.prop.put(attrLine.substring(0,ind), attrLine.substring(ind+1));
                    }
                    backups.add(bkup);
                    atts.close();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            done.append("done");
        }
        setListAdapter( new StableArrayAdapter(this,backups));
    } catch (InterruptedException e) {
        //TODO:errors
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (TimeoutException e) {
        e.printStackTrace();
    }
尽管在for (String app : apps) {它之前有 waitforfinish() ,但它导致了异常。
这个更新的代码应该修复它,从输出中添加数据,并等待主代码中同步的任何落后者,但如果你在上面的 //oh no 行设置断点,它仍然会到达它的位置尝试在 UI 主代码运行后添加项目。所以 waitforfinish() 不是在等待吗?如何防止这种竞争状况?
我也尝试了RootTask下面的代码,但它似乎停在了最后一行?
    RootTask getProfile = new RootTask() {
        @Override
        public void onPostExecute(ArrayList<String> result) {
            super.onPostExecute(result);
            for (String r : result) {
                System.out.println(r);
            }
        }
    };
    getProfile.execute("ls /data/data/org.mozilla.firefox/files/mozilla/" );
onPostExecute 永远不会运行。