0

我在这里遇到了一个非常奇怪的问题。我有一个 PopupWindow 在我的 Moto G、Android 4.4 上运行良好。但现在我正在我的 Galaxy Y Android 2.3.6 上进行测试,似乎 Popup 在其实例化时崩溃了。

我的代码:

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

}

popup 变量是一个全局变量,声明为:

private PopupWindow popup;

我收到的错误堆叠是:

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

我还以正确的方式对其进行了调试,结果只是在new PopupWindow()命令上崩溃了:S

4

1 回答 1

0

也许你NPE是因为 layout 和 popup 没有Context引起的。尝试使用showPopUp(Context context)如下方式附加上下文:

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);
    // ...
}  

然后,当您在上面调用此方法时,请执行以下操作:

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

让我知道这是否有帮助。

于 2014-03-31T00:51:34.640 回答