PartyJat
CJ_Finger
采纳率60%
2016-11-04 09:36

关于Android弹出多个对话框的问题

已采纳

各位大侠好,项目要实现的效果是,先弹出设置密码的对话框,点击确定后,第一个对话框消失,弹出再次确认密码的对话框,可是总是报错

 java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

下面是两个对话框的代码:

 //设置支付密码
    private void setPayPwd1Dlg(){
        AlertDialog.Builder builder = new AlertDialog.Builder(TransferActivity.this);
        builder.setTitle("请设置支付密码");
        builder.setView(et_payp);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.dismiss();
                setPayPwd2Dlg(et_payp.getText().toString());
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });
        builder.show();
    }
    //再次确认支付密码
    private void setPayPwd2Dlg(String paypwd){
        final String paypwd1 = paypwd;
        AlertDialog.Builder builder = new AlertDialog.Builder(TransferActivity.this);
        builder.setTitle("请再次输入支付密码");
        builder.setView(et_payp);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.dismiss();
                if(et_payp.getText().toString().equals(paypwd1)){
                    new Thread(setPayPwd).start();
                }else {
                    Toast.makeText(getApplicationContext(), "两次密码不一致", Toast.LENGTH_SHORT).show();
                }
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });
        builder.show();
    }

请问这个问题要怎么解决?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • oSanYeCao1234567 RoidCoder 5年前

    Can't create handler inside thread that has not called Looper.prepare()

    在子线程中创建Handler之前,必须调用Looper.prepare()来创建Loopger对象

    主线程中默认是有一个Looper对象的,所以可以直接创建Handler

    点赞 评论 复制链接分享
  • CWhy7 CWhy7 5年前

    dialog.dismiss();
    Looper.prepare()
    setPayPwd2Dlg(et_payp.getText().toString());

    这样试下吧,这个是缺少loop,也可能是线程问题

    点赞 评论 复制链接分享