GLSurfaceView的Layout布局报空指针问题

08-04 21:50:30.149 6175-6175/com.example.robotmachine E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.robotmachine, PID: 6175
    java.lang.RuntimeException: Unable to resume activity {com.example.robotmachine/com.example.robotmachine.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.opengl.GLSurfaceView$GLThread.onResume()' on a null object reference
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2991)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3022)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2397)
        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3952)
        at android.app.ActivityThread.access$900(ActivityThread.java:153)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1311)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5293)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.opengl.GLSurfaceView$GLThread.onResume()' on a null object reference
        at android.opengl.GLSurfaceView.onResume(GLSurfaceView.java:561)
        at com.example.robotmachine.Fragment.FragmentOpenGL$override.onResume(FragmentOpenGL.java:51)
        at com.example.robotmachine.Fragment.FragmentOpenGL$override.access$dispatch(FragmentOpenGL.java)
        at com.example.robotmachine.Fragment.FragmentOpenGL.onResume(FragmentOpenGL.java)
        at android.support.v4.app.Fragment.performResume(Fragment.java:2498)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1501)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
        at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3269)
        at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:3241)
        at android.support.v4.app.FragmentController.dispatchResume(FragmentController.java:223)
        at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:538)
        at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:527)
        at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:172)
        at android.app.Activity.performResume(Activity.java:6089)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2980)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3022) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2397) 
        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3952) 
        at android.app.ActivityThread.access$900(ActivityThread.java:153) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1311) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5293) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
    
    
    --------- beginning of system
08-04 21:50:30.216 1446-1446/? E/EGL_emulation: tid 1446: eglCreateSyncKHR(1479): error 0x3004 (EGL_BAD_ATTRIBUTE)

问题出现的原因:

glSurfaceView初始化时候会调用渲染器,所以初始化glSurfaceView时候必须要设置渲染器。
如下所示:

       glSurfaceView = view.findViewById(R.id.opengl_fragmentView);
       glSurfaceView.setRenderer(new GLRenderer(getContext()));

你可能感兴趣的:(OpenGL,Android,Studio)