简体   繁体   English

setvisibility android上的应用程序崩溃

[英]Application crash at setvisibility android

I'm a new developer and I'm encountering a problem when setting the visibility of a layout that covers my UI to disable it while running a script. 我是一名新开发人员,在设置覆盖UI的布局的可见性以在运行脚本时禁用它时遇到了问题。

Here's my code: 这是我的代码:

@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);
    }
}

The problem is that the app crashes as soon as i call the enableLayout() function: 问题是,一旦我调用enableLayout()函数,应用程序就会崩溃:

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

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

Any clues on what the problem might be? 关于可能是什么问题的任何线索?

Any clues on what the problem might be? 关于可能是什么问题的任何线索?

Yes, Issue is occurring accessing UI elements from non UI Thread (calling enableLayout method scriptThread Thread run method). 是,问题已存在的存取UI元素non UI Thread (调用enableLayout方法scriptThread线程中运行方法)。

Use runOnUiThread method for accessing Views from non Ui Thread: 使用runOnUiThread方法从非Ui线程访问视图:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM