Question

I'm having a really weird problem here. I have a PopupWindow working fine on my Moto G, Android 4.4. But now I'm testing on my Galaxy Y Android 2.3.6 and it seems the Popup is crashing on its instantiation.

My code:

// Function called when user hit the send button on screen
public void showPopUp (View v) {
    // Getting location of the popup window
    int [] location = new int[2];
    imgBtn.getLocationOnScreen(location);
    p = new Point();
    p.x = location[0];
    p.y = location[1];

    int popupWidth = 300;
    int popupHeight = 200;

    // Changing button icon
    imgBtn.setBackgroundResource(R.drawable.img_on);

    // Inflate the popup_layout.xml
    LinearLayout viewGroup = (LinearLayout) findViewById(R.id.popup);
    LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = layoutInflater.inflate(R.layout.send_image_popup, viewGroup);
    // Creating the PopupWindow
    popup = new PopupWindow();
    Log.d("POPUP", "dummiedebug");
    popup.setContentView(layout);
    popup.setWidth(popupWidth);
    popup.setHeight(popupHeight);
    popup.setFocusable(true);


    // Some offset to align the popup a bit to the right, and a bit up, relative to button's position.
    int OFFSET_X = 1;
    int OFFSET_Y = 30;

    popup.setBackgroundDrawable(new BitmapDrawable());

    // Displaying the popup at the specified location, + offsets
    popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y - OFFSET_Y - popupHeight);

    // Changing photo ico back to gray
    popup.setOnDismissListener(new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {
            imgBtn.setBackgroundResource(R.drawable.img_off);
        }
    });

}

The popup variable is a global variable declared as:

private PopupWindow popup;

The error stacking Im receiving is:

03-30 20:59:01.398: E/AndroidRuntime(27008): FATAL EXCEPTION: main
03-30 20:59:01.398: E/AndroidRuntime(27008): java.lang.IllegalStateException: Could not execute method of the activity
03-30 20:59:01.398: E/AndroidRuntime(27008):    at android.view.View$1.onClick(View.java:2144)
03-30 20:59:01.398: E/AndroidRuntime(27008):    at android.view.View.performClick(View.java:2485)
03-30 20:59:01.398: E/AndroidRuntime(27008):    at android.view.View$PerformClick.run(View.java:9080)
03-30 20:59:01.398: E/AndroidRuntime(27008):    at android.os.Handler.handleCallback(Handler.java:587)
03-30 20:59:01.398: E/AndroidRuntime(27008):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-30 20:59:01.398: E/AndroidRuntime(27008):    at android.os.Looper.loop(Looper.java:130)
03-30 20:59:01.398: E/AndroidRuntime(27008):    at android.app.ActivityThread.main(ActivityThread.java:3687)
03-30 20:59:01.398: E/AndroidRuntime(27008):    at java.lang.reflect.Method.invokeNative(Native Method)
03-30 20:59:01.398: E/AndroidRuntime(27008):    at java.lang.reflect.Method.invoke(Method.java:507)
03-30 20:59:01.398: E/AndroidRuntime(27008):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
03-30 20:59:01.398: E/AndroidRuntime(27008):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
03-30 20:59:01.398: E/AndroidRuntime(27008):    at dalvik.system.NativeStart.main(Native Method)
03-30 20:59:01.398: E/AndroidRuntime(27008): Caused by: java.lang.reflect.InvocationTargetException
03-30 20:59:01.398: E/AndroidRuntime(27008):    at java.lang.reflect.Method.invokeNative(Native Method)
03-30 20:59:01.398: E/AndroidRuntime(27008):    at java.lang.reflect.Method.invoke(Method.java:507)
03-30 20:59:01.398: E/AndroidRuntime(27008):    at android.view.View$1.onClick(View.java:2139)
03-30 20:59:01.398: E/AndroidRuntime(27008):    ... 11 more
03-30 20:59:01.398: E/AndroidRuntime(27008): Caused by: java.lang.NullPointerException
03-30 20:59:01.398: E/AndroidRuntime(27008):    at android.widget.PopupWindow.setContentView(PopupWindow.java:384)
03-30 20:59:01.398: E/AndroidRuntime(27008):    at android.widget.PopupWindow.<init>(PopupWindow.java:286)
03-30 20:59:01.398: E/AndroidRuntime(27008):    at android.widget.PopupWindow.<init>(PopupWindow.java:266)
03-30 20:59:01.398: E/AndroidRuntime(27008):    at android.widget.PopupWindow.<init>(PopupWindow.java:223)
03-30 20:59:01.398: E/AndroidRuntime(27008):    at br.com.chatApp.activities.Chat.showPopUp(Chat.java:224)
03-30 20:59:01.398: E/AndroidRuntime(27008):    ... 14 more

I also debbuged it on the proper way and the thing just crashes on the new PopupWindow() command :S

Was it helpful?

Solution

Maybe your NPE is caused because layout and popup have not Context. Try to attach the context by using showPopUp(Context context) as follow:

public void showPopUp (Context context) {
    // ...
    LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);  
    LayoutInflater layoutInflater = (LayoutInflater) 
                   context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = layoutInflater.inflate(R.layout.send_image_popup, viewGroup);

    popup = new PopupWindow(context);
    // ...
}  

And then, when you call this method above, do:

// pass your Activity (context)
showPopUp(MyActivity.this);

Let me know if this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top