无法修复WindowManager $ BadTokenException

时间:2018-06-26 11:45:09

标签: android exception dialog

由于WindowManager $ BadTokenException异常,我的应用程序崩溃了。我试图在Internet上弄清楚它。我找到了诸如“将参数更改为Application name.this的答案,我做到了,但是没有成功,然后我注释了整个对话框,然后又遇到了相同的错误。

我要去哪里了

public class srs_tent_booking extends AsyncTask<Void,Void,Void>{
String result_code="";
String message="";


@Override
protected Void doInBackground(Void... voids) {
    System.out.println("SRS_TENT_background==");


 //        runOnUiThread(new Runnable() {
//            @Override
//            public void run() {

            seat_arr = SeatSelectionActivityNew.arr;
            seat_fare = SeatSelectionActivityNew.arr_add_fare;
            System.out.println("aaaaa"+seat_fare.toString());



            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("title_arr[]", title_arr.toString()));
            nameValuePairs.add(new BasicNameValuePair("fname_arr[]", fname_arr.toString()));
            nameValuePairs.add(new BasicNameValuePair("gender_arr[]", gender_arr.toString()));
            nameValuePairs.add(new BasicNameValuePair("age_arr[]", age_arr.toString()));
            nameValuePairs.add(new BasicNameValuePair("seat_arr[]", seat_arr.toString()));
            nameValuePairs.add(new BasicNameValuePair("seat_fare[]",seat_fare.toString()));


            nameValuePairs.add(new BasicNameValuePair("email", email));
            nameValuePairs.add(new BasicNameValuePair("mobile", mobile));
            nameValuePairs.add(new BasicNameValuePair("name", name));
            nameValuePairs.add(new BasicNameValuePair("nos_seats", String.valueOf(nos_seats)));
            nameValuePairs.add(new BasicNameValuePair("board_id", android_board_id));


            nameValuePairs.add(new BasicNameValuePair("srs_city_code", Search_bus_new.srs_city_code));
            nameValuePairs.add(new BasicNameValuePair("srs_city_code1", Search_bus_new.srs_city_code1));
            nameValuePairs.add(new BasicNameValuePair("one_way", Search_bus_new.one_way));
            nameValuePairs.add(new BasicNameValuePair("routeid", String.valueOf(BusOperators.routeid)));
            System.out.println("SRS_TENT_booking=nameValuePairs==="+nameValuePairs.toString());


            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new 
                HttpPost("https://"+Search_bus_new.ip+"/pace/tentative_booking.php");
                //                    HttpPost httpPost = new 
                HttpPost("https://"+Search_bus_new.ip+"/pace/tentative_booking.php");
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpClient.execute(httpPost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
                //Log.e("pass 1", "connection success");
                //Toast.makeText(getApplicationContext(),"success",Toast.LENGTH_SHORT).show();
                System.out.println("SRS_TENT_background=="+"https://"+Search_bus_new.ip+"/pace/tentative_booking.php");
                System.out.println("SRS_TENT_background==Success111");

            } catch (Exception e) {
                Log.e("fail 1", e.toString());
                System.out.println("SRS_TENT_booking=fail1111==="+e.toString());
                Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            }
            try {
                System.out.println("SRS_TENT_background==Success222");

                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
                StringBuilder sb = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");

                }
                is.close();
                result = sb.toString();
                System.out.println("SRS_TENT_booking=result==="+result.toString());

                //Log.e("pass 2","connection success");
                //Toast.makeText(getApplicationContext(),"success",Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                Log.e("fail 2", e.toString());
                System.out.println("SRS_TENT_booking=fail222==="+e.toString());

            }
            JSONArray jsonarray;

            try {
                System.out.println("SRS_TENT_background==Success333");

                JSONObject jsonObj = new JSONObject(result);
                jsonarray = jsonObj.getJSONArray("result");
                System.out.println("SRS_TENT_booking=jsonarray==="+jsonarray.toString());

                for (int i = 0; i < jsonarray.length(); i++) {
                    JSONObject c = jsonarray.getJSONObject(i);
                    Log.e("pass 2", "connection success");

                    result_code = c.getString("result_code");
                    System.out.println("SRS_TENT_booking=result_code==="+result_code.toString());

                    if (result_code.contains("0")) {
                        code = c.getString("code");
                         message = c.getString("message");
                    }
                    if (result_code.contains("1")) {
                        tentative_pnr_number = c.getString("tentative_pnr_number");
                    }


                }


            } catch (JSONException e) {
                e.printStackTrace();
                Log.i("tagconvertstr", "[" + result + "]");
            }


//            }
//
//        });

    return null;
}
    protected void onPreExecute() {
        System.out.println("SRS_TENT_POST==onPreExecute==");

        dialog_load.show();
    dialog_load.setCancelable(false);
    dialog_load.setMessage("Please wait....");
//        



  dialog_load.setIndeterminateDrawable(getResources().
  getDrawable(R.drawable.preloader, null));
  dialog_load.getWindow().setBackgroundDrawable(new 
  ColorDrawable(android.graphics.Color.TRANSPARENT));
    dialog_load.setContentView(R.layout.loading_screen);
    super.onPreExecute();
}
protected void onPostExecute(Void v)
{
    dialog_load.dismiss();
    //Toast.makeText(UserInformation.this, "result_code="+result_code, Toast.LENGTH_SHORT).show();
    try {
        System.out.println("SRS_TENT_POST==");
        if (result_code.contains("0")) {
            System.out.println("SRS_TENT_POST==00000000000000");
        //            final Dialog dialog = new Dialog(UserInformation.this);
            //            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            //            dialog.setCancelable(false);
            //            dialog.setCanceledOnTouchOutside(false);
            //            dialog.setContentView(R.layout.error_mail_dialog);
            //            dialog.getWindow().getAttributes().windowAnimations = 
             R.style.DialogAnimation;
            //            dialog.setTitle("Warning");
            //            TextView text = (TextView) dialog.findViewById(R.id.msg);
            //            text.setText("Server Response" + " " + 
            String.valueOf(message));
            //
             //            Button dialogButton = (Button) 
            dialog.findViewById(R.id.button2);
            //            // if button is clicked, close the custom dialog
             //            dialogButton.setOnClickListener(new View.OnClickListener() {
            //                @Override
           //                public void onClick(View v) {
           //                    dialog.dismiss();
           //                    ProgressDialog dialog;
           //                    dialog = new ProgressDialog(UserInformation.this);
           //                    dialog.show();
           //                    dialog.setMessage("Please wait....");
            ////                                    
         dialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.bus_loader, null));
            //                    dialog.getWindow().setBackgroundDrawable(new 
             ColorDrawable(android.graphics.Color.TRANSPARENT));
            //                    dialog.setContentView(R.layout.loading_screen);
            //
            //                    Intent i1 = new Intent(UserInformation.this, 
            BusOperators.class);
            //                    startActivity(i1);
            //                }
            //            });
           //
           //            dialog.show();
           //            dialog.getWindow().setLayout(ViewGroup.LayoutParams.FILL_PARENT, 
             ViewGroup.LayoutParams.FILL_PARENT);
        }


        if (result_code.contains("1")) {
            System.out.println("SRS_TENT_POST==11111111111111");
            Intent i1 = new Intent(UserInformation.this, Login.class);
            startActivity(i1);
            //confirm_booking();
        }

    }catch(Exception e){
        System.out.println("SRS_TENT_POST==Exception==");
    }


}

}

更新:::

这是我的logcat详细信息

          06-26 17:23:29.270 17127-17127/? E/AndroidRuntime: FATAL EXCEPTION: main
     Process: com.pace.administrator.pacetravels, PID: 17127
     android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@883e611 is not valid; is your activity running?
     at android.view.ViewRootImpl.setView(ViewRootImpl.java:697)
     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:347)
     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:94)
     at android.widget.Toast$TN.handleShow(Toast.java:463)
     at android.widget.Toast$TN$2.handleMessage(Toast.java:346)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:163)
     at android.app.ActivityThread.main(ActivityThread.java:6221)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)

1 个答案:

答案 0 :(得分:0)

异常告诉您:“活动”未运行,因此无法显示吐司,因为没有活动窗口可以吸引。

当您不与主线程同步时,请确保不要在后台线程(例如AsyncTask)上调用任何UI函数。

所以这敬酒:

} catch (Exception e) {
            Log.e("fail 1", e.toString());
            System.out.println("SRS_TENT_booking=fail1111==="+e.toString());
            Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
        }
当应用程序在后台运行时,

将失败。您应该推迟此敬酒,并在onPostExecute中进行处理。当您要在主线程上处理一个doInBackground时,请退出Exception