0

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
João Menighin
  • 3,083
  • 6
  • 38
  • 80

1 Answers1

0

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.

Blo
  • 11,903
  • 5
  • 45
  • 99
  • Thanks dude, it worked. Instead of passing the context I just put ´Chat.this´ instead of the ´context´ variable. I didn't get why this happens, though, since this function is inside my Activity class. Do you bother explain? Thanks a lot :) – João Menighin Mar 31 '14 at 01:16
  • Yes, you can get Context with *Activity.this*, *this* or *getApplicationContext()*..as your wish! Also it must be related to a context because your public method can be call anywhere from your application, and you always need to inflate a layout from a Context (so it's better to use a variable). Anyway, without context, your layout can't be inflated. Finally, *I think* you didn't get this, even if your method is in your Activity, because the showPopUp method works with Context context or View view as an argument, see the reference. So you have to do it with the context regardless of its place. – Blo Mar 31 '14 at 02:44
  • Thanks Fllo, but I think we are talking about a bug on the SDK here. I tried to create the PopupWindow on the onCreate method and it is still like that: new PopupWindow() crashes and new PopupWindow(Activity.this) works on Android 2.3.6 (on 4.4 works fine either way). Im just saying so if we encounter this again on the future. Anyway, thanks again. – João Menighin Mar 31 '14 at 03:05
  • 1
    I meant PopupWindow method instead of showPopUp (http://developer.android.com/reference/android/widget/PopupWindow.html#PopupWindow(android.content.Context)). It works with Context since API1, so I don't think this is a bug, it was built like this. But perhaps you're right, in new SDK, Google has change this method.. Glad to help, it was also a good training to me too ;) – Blo Mar 31 '14 at 03:13