【问题标题】:BadTokenException in asynctask class for adding progressdialog in Tabs?用于在选项卡中添加进度对话框的异步任务类中的 BadTokenException?
【发布时间】:2014-07-12 13:45:27
【问题描述】:

大家好,在我的应用程序中,我正在 asynctask 类中添加进度对话框,如下所示。这个 asynctask 在 oncreate 方法中调用。

class GettingData extends AsyncTask<URL, Integer, Long>
        { 

protected void onPreExecute() 
            {
                dialog=MyProgressDialog.show(getParent(), null,null);
            }
@Override
            protected Long doInBackground(URL... arg0) {
                          try{


                             DefaultHttpClient httpClient = new DefaultHttpClient(); 
                             HttpGet httpPost = new HttpGet(DataUrls.map);
HttpResponse httpResponse = httpClient.execute(httpPost); 
                             HttpEntity httpEntity = httpResponse.getEntity(); 
                             String response = EntityUtils.toString(httpEntity);
}catch(Exception e){
                             e.printStackTrace(); 
                          }

                return null;
            }
            protected void onPostExecute(Long result) {             
                try{
                Map_ExpandableAdapter mNewAdapter = new Map_ExpandableAdapter(MapViewActivity.this,groupItem, childItem,lstMenus,places);
                mNewAdapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),MapViewActivity.this);
                lstMenus.setAdapter(mNewAdapter);
//              lstMenus.setOnChildClickListener(MapViewActivity.this);

                UtilsListViewHeight.setListViewHeightBasedOnChildren(lstMenus);
                }catch(Exception e)
                {
                    e.printStackTrace();
                }
                dialog.dismiss();
            }

        }//closing asynchronous process

这是我正在使用的进度对话框

public class MyProgressDialog extends Dialog {

    public static MyProgressDialog show(Context context, CharSequence title,
            CharSequence message) {
        return show(context, title, message, false);
    }
public MyProgressDialog(Context context) {
        super(context, R.style.NewDialog);
    }
}

在 asynctask 类中我传递了实例 getParent(),因为我正在使用标签。但是我得到了这样的错误

原因:android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@441cf040 无效;您的活动正在进行吗?

所以请告诉我这是什么问题。感谢所有 InAdvanced

【问题讨论】:

    标签: android android-asynctask progressdialog


    【解决方案1】:

    正如错误所说“您的活动是否正在运行”

    您忘记关闭对话框 这些事件可能已经发生

    1. 当您切换到不同的活动而不关闭已显示的对话框时 解决方案 : 在切换到不同的活动之前,请确保对话框已关闭。

    2. 或者当可能发生异常并且您的活动已关闭但对话框未关闭时..

    解决方案: 如我所见,在你的 catch 块中调用dismiss。

    【讨论】:

      【解决方案2】:

      您似乎没有将当前 Activity 作为上下文传递,而是您似乎在传递当前 Activity 的父级。

      替换

      dialog=MyProgressDialog.show(getParent(), null,null);
      

      dialog=MyProgressDialog.show(MyActivity.this, null,null);
      

      其中 MyActivity 是当前活动的名称。

      【讨论】:

      • 我正在使用标签。所以我通过了 getparent()
      • 我当前的活动是子活动
      • 您需要将后执行代码包含在这样的条件中 if( context!=null && !((Activity)context.isFinishing()){ }
      • 但添加进度对话框时出现异常。即 dialog=MyProgressDialog.show(getParent(), null,null);
      • 您能否更新您的代码以包含您在其中编写异步任务的活动/片段
      猜你喜欢
      • 2017-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-17
      • 2012-04-17
      相关资源
      最近更新 更多