Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:Wait()在对话框在单独的线程中获取输入时打开主线程_Java_Android_Multithreading_Wait - Fatal编程技术网

Java Android:Wait()在对话框在单独的线程中获取输入时打开主线程

Java Android:Wait()在对话框在单独的线程中获取输入时打开主线程,java,android,multithreading,wait,Java,Android,Multithreading,Wait,我正在用Android编写一个活动,用户在其中修改SQL数据库。用户界面由用户输入姓名的编辑文本和用户输入此人吸引力的搜索栏组成。下面有一组按钮:添加、编辑、查看、删除 当用户单击“编辑”按钮时,将显示一个输入对话框,要求用户输入记录编号。完成后,将加载该记录 我遇到的问题是,将显示inputdialog,当用户输入记录编号时,编辑方法的其余部分将继续进行,这样当用户输入完输入时,不会发生任何事情,因为功能已经完成 为了解决这个问题,我决定使用多线程(我没有太多的使用经验)。当按下编辑按钮时,主

我正在用Android编写一个活动,用户在其中修改SQL数据库。用户界面由用户输入姓名的编辑文本和用户输入此人吸引力的搜索栏组成。下面有一组按钮:添加、编辑、查看、删除

当用户单击“编辑”按钮时,将显示一个输入对话框,要求用户输入记录编号。完成后,将加载该记录

我遇到的问题是,将显示inputdialog,当用户输入记录编号时,编辑方法的其余部分将继续进行,这样当用户输入完输入时,不会发生任何事情,因为功能已经完成

为了解决这个问题,我决定使用多线程(我没有太多的使用经验)。当按下编辑按钮时,主UI线程被阻止(使用wait()-这是因为我不希望用户输入记录id时UI处于活动状态),并且输入对话框显示在单独的线程中

输入后,将通知线程,其余的编辑功能将继续。(代码如下)

问题是,当我在UI线程上调用wait函数时,我得到一个错误,上面写着“对象未被线程在wait()之前锁定”。如何锁定UI线程?

我知道一般情况下不应该阻止UI线程,但我认为在这种情况下可以,因为我不希望它接受任何用户输入

谢谢你的帮助

public class Attractivometer extends Activity implements OnClickListener {

    private Button      buttonAddRecord,    buttonEditRecord,   buttonSaveChanges;
    private Button      buttonDeleteRecord, buttonViewRecord;
    private EditText    fieldName;
    private SeekBar     seekbarAttractiveness;
    private String      inputFromInputDialog=null;
    private Thread      inputThread;


    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.Attractivometer);

        buttonAddRecord         = (Button) findViewById(R.id.buttonAddRecord);
        buttonSaveChanges       = (Button) findViewById(R.id.buttonSaveChanges);
        buttonEditRecord        = (Button) findViewById(R.id.buttonEditRecord);
        buttonDeleteRecord      = (Button) findViewById(R.id.buttonDeleteRecord);
        buttonViewRecord        = (Button) findViewById(R.id.buttonViewRecord);

        fieldName               = (EditText) findViewById(R.id.fieldName);

        seekbarAttractiveness           = (SeekBar) findViewById(R.id.seekbarAttractiveness);

        buttonAddRecord.setOnClickListener(this);
        buttonSaveChanges.setOnClickListener(this);
        buttonEditRecord.setOnClickListener(this);
        buttonDeleteRecord.setOnClickListener(this);
        buttonViewRecord.setOnClickListener(this);
    }


    public void onClick(View clickedItem)
    {

        switch(clickedItem.getId())
        {
        case R.id.buttonAddRecord:
            //.....
            break;


        case R.id.buttonSaveChanges:
          //... 
            break;

        case R.id.buttonEditRecord:

            inputThread = new Thread(new Runnable(){

                public void run()
                {
                    showInputDialog("Enter Record ID", InputType.TYPE_CLASS_NUMBER);
                }

            });
            inputThread.start();


            try {
                Thread.currentThread().wait();
            } catch (InterruptedException e) {
                Log.e("Attractivometer","Main Thread interrupted while waiting");
                e.printStackTrace();
            }

            try {
                inputThread.join();
            } catch (InterruptedException e) {
                Log.e("Attractivometer","Input Thread interrupted while joining");
                e.printStackTrace();
            }

            int recordId = Integer.parseInt(inputFromInputDialog);
            if(recordId!=null)
            {
                AttractivometerSQLHandler AttractivometerDatabaseHandler = new AttractivometerSQLHandler(this);
                AttractivometerDatabaseHandler.openDatabase();
                String recordName = AttractivometerDatabaseHandler.getName(recordId);
                String recordAttractiveness = AttractivometerDatabaseHandler.getAttractiveness(recordId);

                if(recordName==null || recordAttractiveness==null )
                {
                    //no record found.
                    Toast.makeText(this, "No record with that ID found", Toast.LENGTH_SHORT).show();
                }else
                {
                    fieldName.setText(recordName);
                    seekbarAttractiveness.setProgress( Integer.parseInt(recordAttractiveness) );
                    recordIsOpen(true);
                }


                AttractivometerDatabaseHandler.closeDatabase();
            }else
                //No input.
            recordIsOpen(false);

            break;

        case R.id.buttonDeleteRecord:
            //...
            break;

        case R.id.buttonViewRecord:
            //....

        }
    }

    private void showInputDialog(String prompt, int inputType)
    {
        AlertDialog.Builder inputDialog = new AlertDialog.Builder(this);

        inputDialog.setTitle("Record No.");

        final EditText fieldInput = new EditText(this);
        fieldInput.setInputType(inputType);
        fieldInput.setHint(prompt);

        inputDialog.setView(fieldInput);
        inputDialog.setPositiveButton("OK", new DialogInterface.OnClickListener()
        {

            public void onClick(DialogInterface arg0, int arg1)
            {
                inputFromInputDialog = fieldInput.getText().toString();
                inputThread.notify();

            }
        });

        inputDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
        {

            public void onClick(DialogInterface arg0, int arg1)
            {
                inputFromInputDialog = null;
                inputThread.notify();
            }
        });

        inputDialog.show(); 
    }   
}

不,不,不!不要阻塞UI线程。系统将引发“应用程序未响应”错误。另外,不要试图从非UI线程与用户交互

当用户单击“编辑”时,不要启动编辑方法。只需弹出一个对话框来收集所需信息。添加一个
对话框界面。OnClickListener
到肯定按钮,然后(现在手头有所需的信息)从那里开始编辑方法


有关更多信息,请参阅指南主题。

在调用之前,等待需要像这样获得对象锁:

 synchronized(Thread.currentThread()) { //added
 try {
            Thread.currentThread().wait();
        } catch (InterruptedException e) {
            Log.e("Attractivometer","Main Thread interrupted while waiting");
            e.printStackTrace();
        }
 }

但是为什么要让主线程等待呢?

首先,不应该暂停主线程,因为如果这样做,一切都将冻结,从用户的角度来看,这一点都不好

第二,您应该弹出一个带有两个按钮的对话框,(完成,取消),并允许用户通过按下其中一个按钮来进一步操作。在这里,您可以了解如何显示自定义对话框:

Android多线程编程的第一条规则是,永远不要停止UI线程,所有长时间运行的操作都应该在单独的线程中进行。我所说的长时间运行操作是指SQLite数据库的写入/读取等