为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

温温酱 2022-09-14 17:19:10
我正在尝试显示来自另一个类的警报对话框。我在堆栈溢出中查找了许多问题,但没有一个有效。我有两个类和.我正在尝试显示警报对话框,其中在 中指定。MainActivity.javaCustomInputDialog.javaCustomInputDialog.javaMainActivity.java在我的我有以下代码:MainActivity.javaArrayList<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.javafinal 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
查看完整描述

1 回答

?
猛跑小猪

TA贡献1858条经验 获得超8个赞

问题似乎来自CountDownLatch,它阻塞了线程,你应该在你的自定义对话框类中创建一个接口,在那里你有一个(或者你想调用它的任何内容)你在其他地方实现。onResult()


您必须将一个侦听器传递给您的实现,并在您的OK按钮中调用showDialog()onResult()onClicklistener.onResult()


接口(在自定义输入对话框中.java):


interface CustomInputDialogListener{

    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 anymore

Log.d(TAG, "showDialog: latch count down 1");

您可以删除闩锁创建和尝试/捕获块,并在末尾使用等待和返回语句


//Log.d(TAG, "showDialog: latch created");

//final CountDownLatch latch = new CountDownLatch(1);


/*try {

    Log.d(TAG, "showDialog: latch await");

    latch.await();

} catch (InterruptedException e) {

    e.printStackTrace();

} finally {

    if (result.isEmpty()) {

        return null;

    } else {

        return result;

    }

}*/

在您的主要活动中.java您有2个选择:


实现自定义输入对话框中的接收器并将其传递给showDialog


您的主要活动的标题应如下所示:


public class MainActivity extends AppCompatActivity implements CustomInputDialog.CustomInputDialogListener {

    ...

}

并且您必须在搜索结果() 上实现:


@Override

public void onResult(ArrayList<String> result) {

    this.result = result;

    doThings();

}

当你调用显示方言()时,你传递这个:


CustomInputDialog.showDialog(Test14dialog.this, "Title", fields, this);

您直接实现 onResult :


CustomInputDialog.showDialog(Test14dialog.this, "Title", fields, new CustomInputDialog.CustomInputDialogListener() {

        @Override

        public void onResult(ArrayList<String> result) {

            this.result = result;

            doThings();

        }

    });

显示对话框时不应阻止线程


查看完整回答
反对 回复 2022-09-14
  • 1 回答
  • 0 关注
  • 109 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号