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);
}
});
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With