Android: 更改屏幕方向时出现窗口泄漏的对话框问题

6

我有两个类,一个是适配器,一个是活动。我在适配器中显示了一个对话框。当我尝试更改屏幕方向时,出现错误。我尝试在我的活动中覆盖下面的代码,但似乎没有任何作用。

@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();
        }

非常感谢您的帮助。


你能否也发布你的清单文件? - user948620
@Glenn 这是我在清单文件中的活动行:<activity android:name=".activity.AddressPopupActivity" android:theme="@android:style/Theme.Translucent" android:configChanges="keyboardHidden|orientation|screenSize|screenLayout|uiMode"/>。 - teekib
我必须在onDestroy回调中使用dismiss()关闭我的对话框,当设备方向改变时会调用该回调。然后,在新的方向中,在onCreate中重新显示dialog,如果需要的话。 - Joshua Pinter
3个回答

3

我该如何在适配器中关闭对话框?我认为我必须在活动中关闭,但是对话框在适配器中。 - teekib
1
您可以使用该对象来关闭对话框,例如:my_dialog.dismiss()。 - Chintan Khetiya
告诉我你的对话对象是什么。不需要示例。 - Chintan Khetiya
我需要在Activity中获取Dialog的引用吗? - teekib
首先看对话框示例,然后尝试在您的主要代码中实现,这样对您来说更好。否则我的答案是不正确的,因为我曾经犯过同样的错误。 - Chintan Khetiya
显示剩余5条评论

2
根据chintan/Stephan的提示,我将AlertDialog引用设置为Activity全局变量,并在onDestroy()中进行了dismiss():
if (ad!=null) {
 ad.dismiss();
 ad=null;
}

旋转时不再出现已登录泄漏错误。

我不想考虑那些configChanges参数,因为如果您已经针对横向或其他特定布局有特定的布局,则会使事情混乱。


-3
一个解决此问题的变通方法是添加


android:configChanges="orientation"

在清单文件中为您的活动进行配置。


2
我认为你应该像这样添加 android:configChanges="orientation|screenSize" 以适配 Honeycomb 及以上版本。 - mihail
已经像这样...<activity android:name=".activity.AddressPopupActivity" android:theme="@android:style/Theme.Translucent" android:configChanges="keyboardHidden|orientation|screenSize|screenLayout|uiMode"/> - teekib

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接