5
        public void showPopup(int group,int img_index,JSONArray json_ar,View v){

        PopupMenu pm=new PopupMenu(EditPhotosActivity.this,v);
        pm.getMenuInflater().inflate(R.menu.popup_menu, pm.getMenu());          
        pm.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Toast.makeText(getBaseContext(),"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();  
                return false;
            }
        });
        pm.show();
    }

The showPopup method is being called by an onclick of dynamically created Imagevew. App is getting crashed while executing pm.show().

This is the Error Log.

FATAL EXCEPTION: main
 java.lang.RuntimeException: Binary XML file line #17: You must supply a layout_height attribute.
    at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491)
    at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:5709)
    at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:5850)
    at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:610)
    at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:554)
    at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:56)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:477)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    at android.support.v7.internal.view.menu.MenuPopupHelper$MenuAdapter.getView(MenuPopupHelper.java:335)
    at android.support.v7.internal.view.menu.MenuPopupHelper.measureContentWidth(MenuPopupHelper.java:190)
    at android.support.v7.internal.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:128)
    at android.support.v7.internal.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:102)
    at android.support.v7.widget.PopupMenu.show(PopupMenu.java:108)
    at com.newagesmb.version3.EditPhotosActivity$UserAlbum.showPopup(EditPhotosActivity.java:379)
    at com.newagesmb.version3.EditPhotosActivity$UserAlbum$1.onClick(EditPhotosActivity.java:246)
    at android.view.View.performClick(View.java:4212)
    at android.view.View$PerformClick.run(View.java:17476)
    at android.os.Handler.handleCallback(Handler.java:800)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5371)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    at dalvik.system.NativeStart.main(Native Method)

And this is the Layout

    <LinearLayout 
        android:id="@+id/face_outer"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:layout_weight=".2">
        <RelativeLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/darker_gray">
            <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/photo_face"
            android:paddingLeft="10dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"/>
            <ImageButton 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:src="@drawable/btn_add_photo"
                android:textColor="@android:color/white"
                android:padding="3dp"
                android:id="@+id/add_face"/>
        </RelativeLayout>

        <HorizontalScrollView 
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:id="@+id/face_scroll"
            android:background="@android:color/white"
            android:padding="2dp">
            <LinearLayout
                android:layout_height="match_parent"
                android:layout_width="wrap_content"
                android:id="@+id/face_linear"
                android:orientation="horizontal" >
            </LinearLayout>


        </HorizontalScrollView>

    </LinearLayout>

/This is the piece of code from where showPopup method is called/

                LinearLayout ll_face=(LinearLayout) findViewById(R.id.face_linear);
            // The above line of code is written inside onCreate method

            ImageView imageView1=new ImageView(EditPhotosActivity.this);                                    
            imageView1.setPadding(3,3, 3,3);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);

            imageView1.setLayoutParams(layoutParams);
            ll_face.addView(imageView1);

            img_loader.DisplayImage(tmp_json_array.getJSONObject(j).getString("thumb_image"),imageView1);
            final int group=i;
            final int img_index=j;
            imageView1.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    showPopup(group,img_index,json_ar,v);

                }
            });
user3323403
  • 51
  • 1
  • 3
  • Can you post your error log and your xml layout? – Giacomoni Apr 02 '14 at 18:50
  • The error would seem to suggest that the problem is not with your dynamically created ImageView but rather with a layout inflated from an XML file. Which file is probably mentioned in the log as well, but even if it is not you should be able to figure that out from program context. – Chris Stratton Apr 02 '14 at 19:33
  • @ChrisStratton - Cant be possible that layout_height params are missing for the dynamic ImageView? Just wondering? – Atul O Holic Apr 03 '14 at 11:09
  • You can check my solution, I had to face the same issue [Click here to check - Solution](https://stackoverflow.com/a/55297752/4489222) – Vivek Hande Mar 22 '19 at 10:42

4 Answers4

17

Make sure your have set right theme. android.support.v7.widget.PopupMenu expect the Application to have AppCompat theme, else it will throw exception when you try to show the PopupMenu

 android:theme="@style/Theme.AppCompat"

But, android.widget.PopupMenu will work without this theme.

Libin
  • 16,967
  • 7
  • 61
  • 83
8

Although I was using AppCompat Theme, but I still had the same problem

I found out that problem is in the declaration of the PopupMenu, and the context I am passing to it.

I was using it like that:

PopupMenu popup = new PopupMenu(getApplicationContext(),v)

But it should be like this:

 PopupMenu popup = new PopupMenu(MyActivity.this,v)
Nobel
  • 1,485
  • 1
  • 16
  • 19
1

I had the same issue when creating PopupMenu. I determined that PopupMenu was using:

import android.support.v7.widget.PopupMenu;

The fix was to edit it to:

import android.widget.PopupMenu;
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
0

I think this might be happening because you are using getBaseContext() at Toast.makeText . Try using getApplictaionContext() or EditPhotosActivity.this

tomoima525
  • 832
  • 10
  • 13