所有程序员的好日子。 当我调用 PopupWindow 的方法 showAtLocation 时,我遇到了 NullPointerException 的问题。正如在许多论坛中编写的那样,发生此异常是因为方法 showAtLocation 的第一个参数为null。所以我这样检查一下:
showAtLocation (View parent, int gravity, int x, int y)
parent.equals(null) //- it returns false
LinearLayout lout = (LinearLayout) parent;
lout.getChildCount() //- it returns true count of child elements
((TextView) lout.getChildAt(1)).getText() //- it returns a text which i write in android:text field
我有一个 Gridview ,其适配器是 CustomAdapter (扩展BaseAdapter )。在此类(CustomAdapter)中, getView 方法中包含 OnClickListener 。我想为此gridview的每个项目设置popupwindow。所以在OnClickListener中我调用了一个方法 showPopup :
private void showPopup(final Activity context, Point p) {
int popupWidth = 200;
int popupHeight = 150;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//LayoutInflater layoutInflater = prnt.getLayoutInflater();
View parent = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(parent);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
int OFFSET_X = 30;
int OFFSET_Y = 30;
popup.setBackgroundDrawable(new BitmapDrawable());
/*
LinearLayout lout = (LinearLayout) parent;
showMsg(parent.equals(null) + " : type " + lout.getChildCount() + " - " + ((TextView) lout.getChildAt(1)).getText());
*/
popup.showAtLocation(parent, 0, p.x + OFFSET_X, p.y + OFFSET_Y); //error occurs here
}
拜托,我需要你的帮助
答案 0 :(得分:-2)
02-10 13:49:34.148 2376-2376/com.iyb.wi.mobi I/art: Not late-enabling -Xcheck:jni (already on)
02-10 13:49:34.255 2376-2376/com.iyb.wi.mobi W/System: ClassLoader referenced unknown path: /data/app/com.iyb.wi.mobi-2/lib/x86
02-10 13:49:34.331 2376-2376/com.iyb.wi.mobi I/GMPM: App measurement is starting up, version: 8487
02-10 13:49:34.331 2376-2376/com.iyb.wi.mobi I/GMPM: To enable debug logging run: adb shell setprop log.tag.GMPM VERBOSE
02-10 13:49:34.501 2376-2392/com.iyb.wi.mobi D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
02-10 13:49:34.582 2376-2392/com.iyb.wi.mobi I/OpenGLRenderer: Initialized EGL, version 1.4
02-10 13:49:34.639 2376-2392/com.iyb.wi.mobi W/EGL_emulation: eglSurfaceAttrib not implemented
02-10 13:49:34.639 2376-2392/com.iyb.wi.mobi W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad6dfbc0, error=EGL_SUCCESS
02-10 13:49:45.041 2376-2390/com.iyb.wi.mobi I/GMPM: Tag Manager is not found and thus will not be used
02-10 13:50:05.590 2376-2386/com.iyb.wi.mobi I/art: Background sticky concurrent mark sweep GC freed 11567(906KB) AllocSpace objects, 10(200KB) LOS objects, 22% free, 2MB/3MB, paused 25.010ms total 151.250ms
02-10 13:50:05.670 2376-2392/com.iyb.wi.mobi W/EGL_emulation: eglSurfaceAttrib not implemented
02-10 13:50:05.670 2376-2392/com.iyb.wi.mobi W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad6e5d00, error=EGL_SUCCESS
02-10 13:50:08.094 2376-2376/com.iyb.wi.mobi I/Choreographer: Skipped 141 frames! The application may be doing too much work on its main thread.
02-10 13:50:08.165 2376-2392/com.iyb.wi.mobi E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab793b90
02-10 13:50:34.728 2376-2392/com.iyb.wi.mobi W/EGL_emulation: eglSurfaceAttrib not implemented
02-10 13:50:34.728 2376-2392/com.iyb.wi.mobi W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa1c51100, error=EGL_SUCCESS
02-10 13:50:35.582 2376-2392/com.iyb.wi.mobi E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab793c00
02-10 13:50:35.885 2376-2392/com.iyb.wi.mobi W/EGL_emulation: eglSurfaceAttrib not implemented
02-10 13:50:35.885 2376-2392/com.iyb.wi.mobi W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa1c51d00, error=EGL_SUCCESS
02-10 13:50:38.395 2376-2392/com.iyb.wi.mobi E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab793c70
02-10 13:50:45.007 2376-2392/com.iyb.wi.mobi W/EGL_emulation: eglSurfaceAttrib not implemented
02-10 13:50:45.007 2376-2392/com.iyb.wi.mobi W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa17bad20, error=EGL_SUCCESS
02-10 13:50:45.947 2376-2392/com.iyb.wi.mobi E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb3fd6830
02-10 13:50:46.023 2376-2386/com.iyb.wi.mobi I/art: Background sticky concurrent mark sweep GC freed 3519(315KB) AllocSpace objects, 2(40KB) LOS objects, 0% free, 4MB/4MB, paused 12.041ms total 40.080ms
02-10 13:50:50.583 2376-2376/com.iyb.wi.mobi D/AndroidRuntime: Shutting down VM
02-10 13:50:50.583 2376-2376/com.iyb.wi.mobi E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.iyb.wi.mobi, PID: 2376
java.lang.NullPointerException: Attempt to read from field 'int android.graphics.Point.x' on a null object reference
at com.iyb.wi.mobi.CustomAdapter.showPopup(CustomAdapter.java:136)
at com.iyb.wi.mobi.CustomAdapter.access$000(CustomAdapter.java:23)
at com.iyb.wi.mobi.CustomAdapter$1.onClick(CustomAdapter.java:84)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)