2015-11-04 110 views
-1

我是一名新開發人員,在設置覆蓋我的UI的佈局的可見性以在運行腳本時禁用它時遇到問題。setvisibility中的應用程序崩潰android

這裏是我的代碼:

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 

    switch (item.getItemId()) { 
     case R.id.action_settings: 
      return true; 

     case R.id.action_loadfile: 
      saveTellNamesArray(); 
      LoadFile(txtFolder, ScriptArray); 
      return true; 

     case R.id.action_runscript: 

      disableLayout(); 

      stopScript = false; 

      scriptThread = new Thread(new Runnable() { 
       @Override 
       public void run() { 
        while (!scriptThread.isInterrupted() && !stopScript) { 
         runScript(); 
        } 
        if (stopScript)//revisar 
         enableLayout(); 
       } 
      }); 
      scriptThread.start(); 

      //enableLayout(); 
      return true; 

     case R.id.action_stopscript: 
      if (scriptThread != null) { 
       //scriptThread.interrupt(); 
       stopScript = true; 
      } 
      return true; 

     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 

的問題是,應用程序,只要我調用enableLayout()函數崩潰:

public void disableLayout() { 
    uiBlockLayout.setVisibility(FrameLayout.VISIBLE); 
    uiBlockLayout.bringToFront(); 
} 

public void enableLayout() { 
    uiBlockLayout.setVisibility(FrameLayout.GONE); 
} 

什麼問題可能是任何線索?

+4

請,** **份額的**記錄錯誤**。 – Nawako

+0

Nawako說了些什麼,再加上'uiBlockLayout'被賦值的範圍。 – Mena

+0

謝謝大家,由@ρяσѕρєяK給出的答案是正確的。感謝您的時間。 – Sombrero22

回答

4

任何有關問題的線索?從non UI Thread

是,問題已存在的存取UI元素(主叫enableLayout方法scriptThread線程中運行方法)。

使用runOnUiThread方法訪問非UI線程瀏覽:

if (stopScript){ 
    runOnUiThread(new Runnable() { 
    @Override 
    public void run() { 
     enableLayout(); 
    } 
    }); 
} 
+0

@Mena:嗨,API級別不可能?謝謝 –

+1

@Mena什麼取決於API級別? runOnUiThread從API 1 – Mike

+0

@Mena:肯定.... :) –