I have a ImageButton and i want to use it in a certain Fragment class of my application. But when i use it in my fragment class the app crashes. BUT i can use it in my MainActivity without crashing. How can i fix this?
This is the code i'm using in my fragment class
public void showPopup(View v) {
        PopupMenu popup = new PopupMenu(activity, v);
        MenuInflater inflater = popup.getMenuInflater();
        inflater.inflate(R.menu.song_popup, popup.getMenu());
        popup.show();
    }
This results in a app crash.But when i use this in my MainActivity (in mainactivity "activity" is replaced by this.getApplicationContext) it works.
My button xml
<ImageButton
                android:id="@+id/popUp"
                android:layout_width="20dp"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:layout_marginRight="2dp"
                android:background="@null"
                android:src="@drawable/abc_ic_menu_moreoverflow_mtrl_alpha"
                android:onClick="showPopup" />
LOGCAT
java.lang.IllegalStateException: Could not find a method showPopup(View) in the activity class com.fm.etunes.phone.MainActivity for onClick handler on view class android.widget.ImageButton with id 'popUp'
            at android.view.View$1.onClick(View.java:3814)
            at android.view.View.performClick(View.java:4442)
            at android.view.View$PerformClick.run(View.java:18473)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NoSuchMethodException: showPopup [class android.view.View]
            at java.lang.Class.getConstructorOrMethod(Class.java:472)
            at java.lang.Class.getMethod(Class.java:857)
            at android.view.View$1.onClick(View.java:3807)
            at android.view.View.performClick(View.java:4442)
            at android.view.View$PerformClick.run(View.java:18473)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
            at dalvik.system.NativeStart.main(Native Method)
 
    