Android --- java.lang.RuntimeException: Can‘t create handler inside thread that has not called Loop
生活随笔
收集整理的這篇文章主要介紹了
Android --- java.lang.RuntimeException: Can‘t create handler inside thread that has not called Loop
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
報錯信息如下:
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() {@Overridepublic void onClick(DialogInterface dialog, int which) {HttpUtils.getInstance().postMethod(BaseConfig.APP_ID+"/AssociationEnrollApp/deleteAssociation/"+associationEnrollId+"/"+ associationId, new HttpUtils.HttpListener() {@Overridepublic void requestFinish(String response) {Result<User> result = new Result<>();Type type = new TypeToken<Result<User>>() {}.getType();//解決Gson解析時出現的bugresult = new Gson().fromJson(response, type);Log.i("請求數據:",result.toString());if(result.getStatus().equals("200")) {JUtils.ToastInLoop("撤銷成功!");}else {JUtils.ToastInLoop("撤銷失敗!");}}@Overridepublic void requestError(Exception e) {}});}});//取消builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MyEnrollAssociationInfoActivity.this,"已取消",Toast.LENGTH_LONG).show();}});builder.create();builder.show();解決辦法:
在對話框的外層加上MyEnrollAssociationInfoActivity.this.runOnUiThread
總結
以上是生活随笔為你收集整理的Android --- java.lang.RuntimeException: Can‘t create handler inside thread that has not called Loop的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android——怎么引入okhttp3
- 下一篇: Android --- 在Andoird