“android.view.WindowManager$BadTokenException: Unable to add window” on buider.show()

前端 未结 10 809
忘掉有多难
忘掉有多难 2020-11-22 10:43

From my main activity, I need to call an inner class and in a method within the class, I need to show AlertDialog. After dismissing it, when the OK

10条回答
  •  情话喂你
    2020-11-22 10:59

    • first you cannot extend AsyncTask without override doInBackground
    • second try to create AlterDailog from the builder then call show().

      private boolean visible = false;
      class chkSubscription extends AsyncTask
      {
      
          protected void onPostExecute(String result)
          {
              AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
              builder.setCancelable(true);
              builder.setMessage(sucObject);
              builder.setInverseBackgroundForced(true);
              builder.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int whichButton)
                  {
                      dialog.dismiss();
                  }
              });
      
              AlertDialog myAlertDialog = builder.create();
              if(visible) myAlertDialog.show();
          }
      
          @Override
          protected String doInBackground(String... arg0)
          {
              // TODO Auto-generated method stub
              return null;
          }
      }
      
      
      @Override
      protected void onResume()
      {
          // TODO Auto-generated method stub
          super.onResume();
          visible = true;
      }
      
      @Override
      protected void onStop()
      {
          visible = false; 
          super.onStop();
      }
      

提交回复
热议问题