Sunday, April 20, 2014

[android-developers] What's problem with PopupWindow?

I'm using PopupWindow to develop a Menu style UI. The content view of PopupWindow is created programmatically. The outer view is a scrollview and it has a new LinearLayout, the fragment codes is like:
public class ContainerView extends ScrollView {
  public ContainerView(Context context) {
public ContainerView(Context context, AttributeSet attrs) {
super(context, attrs);

        private void init() {
mContentView = new LinearLayout(getContext());
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams.bottomMargin = mArrowHeight;
addView(mContentView, layoutParams); 
I just want to dynamically create Menu item before showing PopupWindow, so my codes can accept a user customized Menu item layout and inflate it when user call addMenu function.
But I also want to reuse Menu item view if user dismiss PopupWindow and reshow again! So I setVisibility with GONE when PopupWindow is dismissed and visible it when addMenu is called, the codes just like:
public void addMenu(int id, String title, Drawable d) {
View v = mContainerView.getMenuView(mMenus.size());
mMenus.add(new MenuItem(id, title, d));
if (null != v) {
} else {
v = ((Activity)mContext).getLayoutInflater().inflate(mMenuResId, null);
TextView text = (TextView)v.findViewById(mTextResId);
if (mImageResId > 0) {
ImageView img = (ImageView)v.findViewById(mImageResId);
The Menu window can display without any error at the first time, but if dismiss it(touch screen out of PopupWindow) and click button to show it again, the exception is raised. The TextView object in the bold line is null. I debugged and the Menu item view is valid and also contains a valid TextView, but the id of TextView is 0, Why? I don't know who reset the id the TextView. I added breakpoint in onDismiss listener and found the ids of all Menu item view are cleared. I have no idea about it. Could you please help me take a look? How could I fix it?
Thanks a lot!

You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
For more options, visit

No comments:

Post a Comment