Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.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
Android处理程序-工作不正常_Android - Fatal编程技术网

Android处理程序-工作不正常

Android处理程序-工作不正常,android,Android,我想创建一个带有文本字段和按钮的dialogBuilder。这样做的目的是让程序等待任何进一步的操作,直到输入字段中的文本并单击OK按钮。代码如下: private static final Object wait = new int[0]; private static String result = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstan

我想创建一个带有文本字段和按钮的dialogBuilder。这样做的目的是让程序等待任何进一步的操作,直到输入字段中的文本并单击OK按钮。代码如下:

private static final Object wait = new int[0];
private static String result = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Handler h = new Handler();
    final Context context = MainActivity.this;
    h.post(new Runnable() {

        public void run() {
            final Builder dialogBuilder = new AlertDialog.Builder(context);
            dialogBuilder.setTitle(R.string.app_name);
            final LinearLayout panel = new LinearLayout(context);
            panel.setOrientation(LinearLayout.VERTICAL);
            final TextView label = new TextView(context);
            label.setId(1);
            label.setText(R.string.app_name);
            panel.addView(label);

            final EditText input = new EditText(context);
            input.setId(2);
            input.setSingleLine();
            input.setInputType(InputType.TYPE_CLASS_TEXT
                    | InputType.TYPE_TEXT_VARIATION_URI
                    | InputType.TYPE_TEXT_VARIATION_PHONETIC);
            final ScrollView view = new ScrollView(context);
            panel.addView(input);
            view.addView(panel);

            dialogBuilder
                    .setCancelable(true)
                    .setPositiveButton(R.string.app_name,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    result = input.getText().toString();

                                    synchronized (wait) {
                                        wait.notifyAll();
                                    }

                                    dialog.dismiss();
                                }
                            }).setView(view);

            dialogBuilder.setOnCancelListener(new OnCancelListener() {
                public void onCancel(DialogInterface arg0) {
                    result = null;
                    synchronized (wait) {
                        wait.notifyAll();
                    }
                }
            });
            dialogBuilder.create().show();
        }

    });

    String localResult = null;
    try {
        synchronized (wait) {
            Log.d("Waiting", "Waiting " + localResult);
            wait.wait();
        }
        localResult = result;
        result = null;
        if (localResult == null) {
            // user is requesting cancel
            throw new RuntimeException("Cancelled by user");
        }
        Log.d("RESULT ", "RESULT " + localResult);
    } catch (InterruptedException e) {
        localResult = result;
        result = null;

        if (localResult == null) {
            // user is requesting cancel
            Log.d("CANCELED ", "CANCELED " + localResult);
            throw new RuntimeException("Cancelled by user");
        }
    }
    Log.d("RESULT AFTER THE DIALOG", "RESULT AFTER THE DIALOG " + result);
}
程序将记录.d(“Waiting”、“Waiting”+localResult);在那之后就等着吧。“活动”窗口上不显示对话框生成器。我使用了调试模式,看到程序流没有进入run()方法,但Handler.post()的值为true。由于这个原因,对话框没有显示,程序正在等待

我已经尝试删除等待的时刻(删除Handler.post()),只是想看看对话框是否会显示,它显示的很好,移动的很好,但是结果不是我需要的-我希望程序等待对话框的输入。。。我真的没有主意了


你能给我一些建议吗?我真的没有主意了


非常感谢

您应该在UI线程中运行对话框的显示,而不是单独的线程

例如:

在onCreate()中
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
//加载联系人时显示进度对话框
dialog=新建ProgressDialog(此);
//继续配置对话框
}
});
//执行异步任务
新建异步任务(){
@凌驾
受保护的Void doInBackground(Void…参数){
//要在后台执行的代码
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
//完成回接后关闭对话框
如果(对话框!=null)
dialog.dismise();
super.onPostExecute(结果);
}
}.执行((作废[])无效);

具体来说,这里发生的是对话框显示在UI线程上,然后在对话框运行时在后台执行AsyncTask。然后在执行结束时,我们关闭对话框。

处理程序不在单独的线程中运行。因此,当您调用
wait()
时:

它无限期地等待,因为处理程序在与当前线程相同的线程上运行。您的
Runnable
只能在
onCreate()
方法完成后执行,但这永远不会发生,因为您刚刚调用了
wait()


您应该重新考虑您的想法并找到解决方法(例如,只要用户没有输入有效文本,就以通常的方式显示对话框并禁用“确定”按钮)。但是在UI线程上调用
wait()
不会很顺利。

我不确定,但我认为您可能会在处理程序弹出帖子之前等待。实际上,我不明白您为什么要使用处理程序。。具体来说,处理程序用于从一个线程到主线程的通信。你所做的并不涉及。。。所以我有点困惑为什么要使用它。它看起来像是在主线程中运行。处理程序是使用用于创建给定活动的循环器创建的。请尝试将我的代码与此集成,因为我正在尝试,但只有在启动程序后才会出现关闭…这只是一个大纲,我不会为您设计解决方案。非常感谢,伙计-在重新构建我的代码的整个想法之后,并添加异步进程,帮助我解决问题。但关键是,项目结构不适合采取这种行动方式。现在一切都很好,程序按照我的要求运行。非常感谢!!!!很高兴听到“三分之一”,别忘了接受它作为答案!你能给我举个例子或是一个想法吗?这就是解决方案——重新考虑我的想法并使用异步过程。
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // Display progress dialog when loading contacts
                            dialog = new ProgressDialog(this);
                            // continue with config of Dialog
                        }
                    });

                        // Execute the Asynchronus Task
                        new AsyncTask<Void, Void, Void>() {
                            @Override
                            protected Void doInBackground(Void... params) {
                                // code to execute in background
                                return null;
                            }
                            @Override
                            protected void onPostExecute(Void result) {
                                // Dismiss the dialog after inBackground is done
                                if (dialog != null)
                                    dialog.dismiss();

                                super.onPostExecute(result);
                            }

                        }.execute((Void[]) null);
    synchronized (wait) {
        Log.d("Waiting", "Waiting " + localResult);
        wait.wait();
    }