Android:对话框,更改方向时出现“窗口泄露”问题

时间:2013-01-31 09:21:56

标签: android android-alertdialog

我有两个类,一个适配器和一个活动,我有一个dailog,我在适配器中显示。当我试图更改屏幕方向时,我收到错误。我尝试在我的活动中覆盖以下代码。但似乎没有任何作用

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
          Log.d(TAG, "configuration changed : " +newConfig);

    }

下面是我的适配器代码

public AddressPopUpAdapter(Activity activity, Activity parent,
            int resourceId, ArrayList<PopUpMenu> items, int renderer) {
        super(activity, resourceId, items);
        this.items = items;
        this.renderer = renderer;
        this.activity = activity;
        this.parentActivity = parent;
        popupMenuUtils = new PopupMenuUtils();
        dialog = new Dialog(parentActivity);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        popUpMenu = items.get(position);
        Log.d(TAG, "location" + popUpMenu);
        if (popUpMenu == null) {
            return null;
        }
        Log.d(TAG, "location" + popUpMenu);
        View view = convertView;
        if (view == null) {
            LayoutInflater layoutInflater = (LayoutInflater) (getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE));
            view = layoutInflater.inflate(renderer, null);

        }

        final LinearLayout popupmain = (LinearLayout) view
                .findViewById(R.id.popupmain);

        popupmain.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setEnabled(false);
                performAction(v, activity);

            }
        });

        if (position % 2 == 0) {

            view.setBackgroundResource(R.drawable.listshape);

        } else {
            view.setBackgroundResource(R.drawable.favoritebody);
        }
        return view;
    }

    public void performAction(View v, Activity activity) {

        Context myContext = v.getContext();
        PopUpMenu popUpMenu = (PopUpMenu) v.getTag();
        String result = popUpMenu.getMenuName();
        if (result != null
                && result.equalsIgnoreCase(myContext.getResources().getString(
                        R.string.savecurrentlocation))) {
            getCurrentLocation();

        }

        else if (result != null
                && result.equalsIgnoreCase(myContext.getResources().getString(
                        R.string.distancebetween))) {
            AttractionService service = AttractionService
                    .getInstance(parentActivity.getApplicationContext());
            ArrayList<AttractionData> allMenuSearchList = service
                    .getAllAttractions(true, Constants.field, Constants.order);

            if (allMenuSearchList != null && !allMenuSearchList.isEmpty()) {

                dialog.setContentView(R.layout.pickdestination);
                ListView listPlace = (ListView) dialog
                        .findViewById(R.id.listPlace);
                PlaceAdapter placeAdapter = new PlaceAdapter(parentActivity,
                        allMenuSearchList, R.layout.pickcity, dialog,
                        R.string.pickstartingpoint, null);
                listPlace.setAdapter(placeAdapter);
giving error here----------->   dialog.show();

            } else {
                Toast.makeText(parentActivity, R.string.nonavigationtolink,
                        Toast.LENGTH_SHORT).show();

            }

            this.activity.finish();
        }

感谢任何帮助。

3 个答案:

答案 0 :(得分:2)

事情就是你没有解雇()你的对话并完成活动,所以当方向改变时它会得到事先的对话状态。

所以try to dismiss() your dialog before you finish your activity.

See More Detail if any other cause :

答案 1 :(得分:1)

根据chintan / Stephan的上述提示;我将AlertDialog引用设置为Activity的全局,并在onDestroy()中进行了解除:

if (ad!=null) {
 ad.dismiss();
 ad=null;
}

旋转时不再记录泄漏错误。

我不想考虑那些configChanges参数,因为如果你已经有了特定的横向布局,那么它们就会搞砸了。

答案 2 :(得分:-2)

此问题的解决方法是添加

android:configChanges="orientation"

表示你在清单中的活动。