RuntimeException:二进制XML文件行#17:你必须提供一个layout_height属性,显示popupmenu

时间:2014-04-02 18:39:39

标签: android popupmenu

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

动态创建的Imagevew的onclick正在调用showPopup方法。应用程序在执行pm.show()时崩溃。

这是错误日志。

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)

这就是布局

    <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>

/ 这是一段代码,其中showPopup方法被称为 /

                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);

                }
            });

4 个答案:

答案 0 :(得分:17)

确保您设置了正确的主题。 android.support.v7.widget.PopupMenu期望Application具有AppCompat主题,否则当您尝试显示PopupMenu时会抛出异常

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

但是,android.widget.PopupMenu将在没有此主题的情况下发挥作用。

答案 1 :(得分:7)

虽然我使用的是AppCompat主题,但我仍然遇到了同样的问题

我发现问题出在PopupMenu的声明中,以及我传递给它的上下文。

我正在那样使用它:

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

但它应该是这样的:

 PopupMenu popup = new PopupMenu(MyActivity.this,v)

答案 2 :(得分:1)

创建PopupMenu时遇到了同样的问题。我确定PopupMenu正在使用:

import android.support.v7.widget.PopupMenu;

修复方法是将其编辑为:

import android.widget.PopupMenu;

答案 3 :(得分:0)

我认为这可能会发生,因为您在getBaseContext()使用Toast.makeText。尝试使用getApplictaionContext()EditPhotosActivity.this