在弹出菜单 "InvocationTargetException" 中出现 运行 时间错误

Getting a run time error in popup menu "InvocationTargetException"

在我的应用程序中,我想显示弹出菜单,我试过了,但出现 运行 时间错误,无法修复此问题 error.In 我不想使用我的应用程序一个 ActionBar,我附上下面的代码看看它

HomeScreen.class

public class HomeScreen extends Activity {

TextView reg;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_screen);
}



public void popUpMenu(View v) {
    // TODO Auto-generated method stub
    PopupMenu popup= new PopupMenu(getApplicationContext(), v);
    popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {

              Toast.makeText(HomeScreen.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();  

            return true;
        }
    });
    popup.show();

}

}

activity_home_screen.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/splash_backronud"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="48dp"
    android:background="#959595"
    android:orientation="horizontal" >

    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="3dp"
        android:background="@drawable/icon" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|center_vertical"
        android:layout_marginLeft="25dp"
        android:text="@string/app_name"
        android:textColor="#ffffff"
        android:textSize="18sp"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:gravity="right"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:layout_marginRight="10dp"
            android:background="@drawable/help"
            android:onClick="onClickHelp" />

        <Button
            android:id="@+id/btnpopupMenu"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_gravity="center"
            android:layout_marginRight="10dp"
            android:background="@drawable/menu"
            android:onClick="popUpMenu" />
    </LinearLayout>
</LinearLayout>
</LinearLayout>

popup_menu

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/home"
    android:title="@string/home"/>
<item android:id="@+id/settings"
    android:title="@string/action_settings"/>
<item android:id="@+id/help"
    android:title="@string/help"/>
<item android:id="@+id/profile"
    android:title="@string/profile"/>

AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.om.timetracker"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".WelcomeScreen"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.om.timetracker.HomeScreen"
            android:windowSoftInputMode="stateHidden" >
        </activity>


</manifest>

运行时错误

    05-18 16:18:33.509: E/AndroidRuntime(4053): FATAL EXCEPTION: main
05-18 16:18:33.509: E/AndroidRuntime(4053): java.lang.IllegalStateException: Could not execute method of the activity
05-18 16:18:33.509: E/AndroidRuntime(4053):     at android.view.View.onClick(View.java:3099)
05-18 16:18:33.509: E/AndroidRuntime(4053):     at android.view.View.performClick(View.java:3571)
05-18 16:18:33.509: E/AndroidRuntime(4053):     at android.view.View$PerformClick.run(View.java:14247)
05-18 16:18:33.509: E/AndroidRuntime(4053):     at android.os.Handler.handleCallback(Handler.java:605)
05-18 16:18:33.509: E/AndroidRuntime(4053):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-18 16:18:33.509: E/AndroidRuntime(4053):     at android.os.Looper.loop(Looper.java:137)
05-18 16:18:33.509: E/AndroidRuntime(4053):     at android.app.ActivityThread.main(ActivityThread.java:4517)
05-18 16:18:33.509: E/AndroidRuntime(4053):     at java.lang.reflect.Method.invokeNative(Native Method)
05-18 16:18:33.509: E/AndroidRuntime(4053):     at java.lang.reflect.Method.invoke(Method.java:511)
05-18 16:18:33.509: E/AndroidRuntime(4053):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
05-18 16:18:33.509: E/AndroidRuntime(4053):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
05-18 16:18:33.509: E/AndroidRuntime(4053):     at dalvik.system.NativeStart.main(Native Method)
05-18 16:18:33.509: E/AndroidRuntime(4053): Caused by: java.lang.reflect.InvocationTargetException
05-18 16:18:33.509: E/AndroidRuntime(4053):     at java.lang.reflect.Method.invokeNative(Native Method)
05-18 16:18:33.509: E/AndroidRuntime(4053):     at java.lang.reflect.Method.invoke(Method.java:511)
05-18 16:18:33.509: E/AndroidRuntime(4053):     at android.view.View.onClick(View.java:3094)
05-18 16:18:33.509: E/AndroidRuntime(4053):     ... 11 more
05-18 16:18:33.509: E/AndroidRuntime(4053): Caused by: java.lang.RuntimeException: Binary XML file line #17: You must supply a layout_height attribute.
05-18 16:18:33.509: E/AndroidRuntime(4053):     at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491)
05-18 16:18:33.509: E/AndroidRuntime(4053):     at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:5333)
05-18 16:18:33.509: E/AndroidRuntime(4053):     at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:5453)
05-18 16:18:33.509: E/AndroidRuntime(4053):     at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:580)
05-18 16:18:33.509: E/AndroidRuntime(4053):     at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:537)
05-18 16:18:33.509: E/AndroidRuntime(4053):     at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:54)
05-18 16:18:33.509: E/AndroidRuntime(4053):     at android.view.LayoutInflater.inflate(LayoutInflater.java:477)
05-18 16:18:33.509: E/AndroidRuntime(4053):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-18 16:18:33.509: E/AndroidRuntime(4053):     at android.support.v7.internal.view.menu.MenuPopupHelper$MenuAdapter.getView(MenuPopupHelper.java:370)
05-18 16:18:33.509: E/AndroidRuntime(4053):     at android.support.v7.internal.view.menu.MenuPopupHelper.measureContentWidth(MenuPopupHelper.java:219)
05-18 16:18:33.509: E/AndroidRuntime(4053):     at android.support.v7.internal.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:153)
05-18 16:18:33.509: E/AndroidRuntime(4053):     at android.support.v7.internal.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:125)
05-18 16:18:33.509: E/AndroidRuntime(4053):     at android.support.v7.widget.PopupMenu.show(PopupMenu.java:193)
05-18 16:18:33.509: E/AndroidRuntime(4053):     at com.om.timetracker.HomeScreen.popUpMenu(HomeScreen.java:83)
05-18 16:18:33.509: E/AndroidRuntime(4053):     ... 14 more

您的代码很好,只需将按钮放在 HomeScreen.xml 文件中的相对布局、线性布局等容器布局中...

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".HomeScreen">

        <Button
            android:id="@+id/btnpopupMenu"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_gravity="center"
            android:layout_marginRight="10dp"
            android:background="@drawable/menu"
            android:onClick="popUpMenu" />

    </RelativeLayout>