猿问

警报对话框未从其他非活动类显示

我正在尝试显示来自另一个类的警报对话框。我在堆栈溢出中查找了许多问题,但没有一个有效。


我有两个类和.我正在尝试显示警报对话框,其中在 中指定。MainActivity.javaCustomInputDialog.javaCustomInputDialog.javaMainActivity.java


在我的我有以下代码:MainActivity.java


ArrayList<CustomInputDialog.Field> fields = new ArrayList<>(Arrays.asList(

                        new CustomInputDialog.Field(CustomInputDialog.Field.TYPE.TEXT, "Name", "", null),

                        new CustomInputDialog.Field(CustomInputDialog.Field.TYPE.DATE, "Start Date", "", null),

                        new CustomInputDialog.Field(CustomInputDialog.Field.TYPE.DATE, "End Date", "", null)

                ));


                ArrayList<String> result = CustomInputDialog.showDialog(MainActivity.this, "Title", fields);

在我的我有以下代码:CustomInputDialog.java


final class CustomInputDialog {

    private static final String TAG = "CustomInputDialog";

    private static final String dateUISeparator = " : ";


    static ArrayList<String> showDialog(final Context context, String title, final ArrayList<Field> fields) {

        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);

        final LinearLayout layout = new LinearLayout(context);

        layout.setOrientation(LinearLayout.VERTICAL);

        final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        layoutParams.setMargins(10, 10, 10, 10);

        final ArrayList<View> uis = new ArrayList<>();

        for (final Field field : fields) {

            final View ui;

            switch (field.type) {

                 /**To long code it just creates specified views and saves it in `ui` variable*/

            }

            ui.setLayoutParams(layoutParams);

            Log.d(TAG, "showDialog: ui added");

            layout.addView(ui);

            uis.add(ui);

        }

   

在调试时,事实证明没有抛出异常,但仍然在方法中显示变量 :不可见。alertDialogshowDialog


温温酱
浏览 110回答 1
1回答

猛跑小猪

问题似乎来自CountDownLatch,它阻塞了线程,你应该在你的自定义对话框类中创建一个接口,在那里你有一个(或者你想调用它的任何内容)你在其他地方实现。onResult()您必须将一个侦听器传递给您的实现,并在您的OK按钮中调用showDialog()onResult()onClicklistener.onResult()接口(在自定义输入对话框中.java):interface CustomInputDialogListener{&nbsp; &nbsp; void onResult(ArrayList<String> result);}要传递给显示的新参数:static void showDialog(final Context context, String title, final ArrayList<Field> fields, final CustomInputDialogListener listener) {...}单击“确定”按钮的末尾:”dialog.dismiss();listener.onResult(result);//latch.countDown(); //you don't need that anymoreLog.d(TAG, "showDialog: latch count down 1");您可以删除闩锁创建和尝试/捕获块,并在末尾使用等待和返回语句//Log.d(TAG, "showDialog: latch created");//final CountDownLatch latch = new CountDownLatch(1);/*try {&nbsp; &nbsp; Log.d(TAG, "showDialog: latch await");&nbsp; &nbsp; latch.await();} catch (InterruptedException e) {&nbsp; &nbsp; e.printStackTrace();} finally {&nbsp; &nbsp; if (result.isEmpty()) {&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; }}*/在您的主要活动中.java您有2个选择:实现自定义输入对话框中的接收器并将其传递给showDialog您的主要活动的标题应如下所示:public class MainActivity extends AppCompatActivity implements CustomInputDialog.CustomInputDialogListener {&nbsp; &nbsp; ...}并且您必须在搜索结果() 上实现:@Overridepublic void onResult(ArrayList<String> result) {&nbsp; &nbsp; this.result = result;&nbsp; &nbsp; doThings();}当你调用显示方言()时,你传递这个:CustomInputDialog.showDialog(Test14dialog.this, "Title", fields, this);您直接实现 onResult :CustomInputDialog.showDialog(Test14dialog.this, "Title", fields, new CustomInputDialog.CustomInputDialogListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onResult(ArrayList<String> result) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.result = result;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doThings();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });显示对话框时不应阻止线程
随时随地看视频慕课网APP

相关分类

Java
我要回答