您当前的位置: 首页 >  Java

梁同学与Android

暂无认证

  • 5浏览

    0关注

    610博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Android --- java.lang.RuntimeException: Can‘t create handler inside thread that has not called Loop

梁同学与Android 发布时间:2021-02-18 16:13:35 ,浏览量:5

报错信息如下:

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

意思是无法在尚未调用的线程内创建处理程序 简单的说要在子线程里调用UI

错误代码展示:

AlertDialog.Builder builder = new AlertDialog.Builder(MyEnrollAssociationInfoActivity.this);
                        builder.setTitle("撤销");
                        builder.setMessage("您确定要撤销此社团吗?");
                        //确定
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                HttpUtils.getInstance().postMethod(BaseConfig.APP_ID+"/AssociationEnrollApp/deleteAssociation/"+associationEnrollId+"/"+ associationId, new HttpUtils.HttpListener() {
                                    @Override
                                    public void requestFinish(String response) {
                                        Result result = new Result();
                                        Type type = new TypeToken() {}.getType();//解决Gson解析时出现的bug
                                        result = new Gson().fromJson(response, type);
                                        Log.i("请求数据:",result.toString());
                                        if(result.getStatus().equals("200")) {
                                            JUtils.ToastInLoop("撤销成功!");
                                        }else {
                                            JUtils.ToastInLoop("撤销失败!");
                                        }
                                    }

                                    @Override
                                    public void requestError(Exception e) {

                                    }
                                });
                            }
                        });
                        //取消
                        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MyEnrollAssociationInfoActivity.this,"已取消",Toast.LENGTH_LONG).show();
                            }
                        });
                        builder.create();
                        builder.show();

解决办法: 在对话框的外层加上MyEnrollAssociationInfoActivity.this.runOnUiThread

MyEnrollAssociationInfoActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        AlertDialog.Builder builder = new AlertDialog.Builder(MyEnrollAssociationInfoActivity.this);
                        builder.setTitle("撤销");
                        builder.setMessage("您确定要撤销此社团吗?");
                        //确定
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                HttpUtils.getInstance().postMethod(BaseConfig.APP_ID+"/AssociationEnrollApp/deleteAssociation/"+associationEnrollId+"/"+ associationId, new HttpUtils.HttpListener() {
                                    @Override
                                    public void requestFinish(String response) {
                                        Result result = new Result();
                                        Type type = new TypeToken() {}.getType();//解决Gson解析时出现的bug
                                        result = new Gson().fromJson(response, type);
                                        Log.i("请求数据:",result.toString());
                                        if(result.getStatus().equals("200")) {
                                            JUtils.ToastInLoop("撤销成功!");
                                        }else {
                                            JUtils.ToastInLoop("撤销失败!");
                                        }
                                    }

                                    @Override
                                    public void requestError(Exception e) {

                                    }
                                });
                            }
                        });
                        //取消
                        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MyEnrollAssociationInfoActivity.this,"已取消",Toast.LENGTH_LONG).show();
                            }
                        });
                        builder.create();
                        builder.show();
                    }
                });
关注
打赏
1657180707
查看更多评论
立即登录/注册

微信扫码登录

0.1455s