2016-02-08 1 views
-2

Bonne journée pour tous les programmeurs. J'ai un problème NullPointerException lorsque j'appelle la méthode showAtLocation de PopupWindow. Comme dans beaucoup de forums a été écrit, cette exception se produit parce que le premier paramètre de la méthode showAtLocation est null. Donc, je vérifie cette façon:NullPointErexception lors de l'appel showAtLocation

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 

J'ai un Gridview et son adaptateur est CustomAdapter (étend BaseAdapter). Dans cette classe (CustomAdapter) a OnClickListener dans getView méthode. Je veux définir popupwindow pour chaque élément de cette grille. Donc, en OnClickListener j'appelle une méthode 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 
} 

S'il vous plaît, j'ai besoin de votre aide

+0

double possible de [Qu'est-ce qu'un exception de pointeur nul, et comment puis-je résoudre ce problème?] (http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – soorapadman

Répondre

-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) 
+0

Problème était ici: java.lang.NullPointerException: 'code 'Tentative de lecture du champ 'int android.graphics.Point.x' sur une référence d'objet null – serebrus