我是一名新开发人员,在设置覆盖我的UI的布局的可见性时遇到问题,以便在运行脚本时禁用它。
这是我的代码:
@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);
}
有关问题的任何线索?
答案 0 :(得分:4)
有关问题的任何线索?
是的,问题正在从non UI Thread
访问UI元素(调用enableLayout
方法scriptThread
线程运行方法)。
使用runOnUiThread
方法访问非Ui线程的视图:
if (stopScript){
runOnUiThread(new Runnable() {
@Override
public void run() {
enableLayout();
}
});
}