2016-09-21 3 views
-2

CubeRender 클래스에서 색상 변경 버튼 nullpointer 예외를 클릭하면캐스트 원격 디스플레이 android Nullpointer

java.lang.NullPointerException: Attempt to invoke virtual method 'void CubeRenderer.changeColor()' on a null object reference 

자바 클래스

public class CubeRenderer implements GLSurfaceView.Renderer { 

private static final String TAG = "CubeRenderer"; 

private static final float ANGLE_INCREMENT = 1.2f; 
private static final boolean CALCULATE_FPS = false; 

private Cube mCube; 
private float mAngle; 
private boolean mChangeColor; 
private long mLastTime; 
private long mFpsCounter; 

protected final float[] mMMatrix = new float[16]; 
protected final float[] mMVMatrix = new float[16]; 
protected final float[] mMVPMatrix = new float[16]; 
protected final float[] mProjectionMatrix = new float[16]; 
protected final float[] mViewMatrix = new float[16]; 
protected final float[] mRotationMatrix = new float[16]; 

public void onDrawFrame(GL10 unused) { 
    if (CALCULATE_FPS) { 
     long currentTime = SystemClock.uptimeMillis(); 
     if (mLastTime == 0) { 
      mLastTime = currentTime; 
     } else { 
      mFpsCounter++; 
      long diffTime = currentTime - mLastTime; 
      if (diffTime >= 1000) { 
       Log.d(TAG, "fps=" + mFpsCounter); 
       mFpsCounter = 0; 
       mLastTime = currentTime; 
      } 
     } 
    } 

    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); 

    // Set the camera position 
    Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -10, 0f, 0f, 0f, 0f, 1.0f, 0.0f); 

    // Configure matrices for first cube 
    Matrix.setIdentityM(mMMatrix, 0); 

    Matrix.translateM(mMMatrix, 0, 0.0f, -0.5f, -1.5f); 

    Matrix.setRotateM(mRotationMatrix, 0, 2 * mAngle, 0.0f, 1.0f, 1.0f); 
    Matrix.multiplyMM(mMMatrix, 0, mRotationMatrix, 0, mMMatrix, 0); 

    Matrix.multiplyMM(mMVMatrix, 0, mViewMatrix, 0, mMMatrix, 0); 
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVMatrix, 0); 

    mCube.draw(mMVPMatrix, mChangeColor); 

    // Configure matrices for second cube 
    Matrix.setIdentityM(mMMatrix, 0); 

    Matrix.translateM(mMMatrix, 0, 0.0f, 2.0f, 0.0f); 

    Matrix.setRotateM(mRotationMatrix, 0, -mAngle, 0.0f, 1.0f, 1.0f); 
    Matrix.multiplyMM(mMMatrix, 0, mRotationMatrix, 0, mMMatrix, 0); 

    Matrix.multiplyMM(mMVMatrix, 0, mViewMatrix, 0, mMMatrix, 0); 
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVMatrix, 0); 

    mCube.draw(mMVPMatrix, mChangeColor); 

    mAngle += ANGLE_INCREMENT; 
} 

public void onSurfaceChanged(GL10 unused, int width, int height) { 
    float ratio = (float) width/height; 
    GLES20.glViewport(0, 0, width, height); 

    // Configure perspective with field of view 
    float fov = 30.0f; 
    float near = 1.0f; 
    float far = 100.0f; 
    float top = (float) Math.tan(fov * Math.PI/360.0f) * near; 
    float bottom = -top; 
    float left = ratio * bottom; 
    float right = ratio * top; 
    Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far); 
} 

public void onSurfaceCreated(GL10 unused, EGLConfig config) { 
    // Set background color 
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 

    // Depth handling 
    GLES20.glEnable(GLES20.GL_DEPTH_TEST); 
    GLES20.glDepthFunc(GLES20.GL_LEQUAL); 

    // Set anti-aliasing 
    GLES20.glEnable(GLES20.GL_BLEND); 
    GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA); 

    // Important to initialize the graphics on the GL thread 
    mCube = new Cube(); 
} 

/** 
* Utility method to allow the user to change the cube color. 
*/ 
public void changeColor() { 
    mChangeColor = !mChangeColor; 
} 
} 

액세스 CubeRenderer 클래스 방법 mCubeRenderer.changeColor() 널 포인터 예외

public class PresentationService extends CastRemoteDisplayLocalService { 

private static final String TAG = "PresentationService"; 

// First screen 
private CastPresentation mPresentation; 
private MediaPlayer mMediaPlayer; 
private CubeRenderer mCubeRenderer; 

@Override 
public void onCreate() { 
    super.onCreate(); 
    // Audio 
    mMediaPlayer = MediaPlayer.create(this, R.raw.sound); 
    mMediaPlayer.setVolume((float) 0.1, (float) 0.1); 
    mMediaPlayer.setLooping(true); 
} 

@Override 
public void onCreatePresentation(Display display) { 
    createPresentation(display); 
} 

@Override 
public void onDismissPresentation() { 
    dismissPresentation(); 
} 

private void dismissPresentation() { 
    if (mPresentation != null) { 
     mMediaPlayer.stop(); 
     mPresentation.dismiss(); 
     mPresentation = null; 
    } 
} 

private void createPresentation(Display display) { 
    dismissPresentation(); 
    mPresentation = new TVPresentation(this, display); 

    try { 
     mPresentation.show(); 
     mMediaPlayer.start(); 
    } catch (WindowManager.InvalidDisplayException ex) { 
     Log.e(TAG, "Unable to show presentation, display was removed.", ex); 
     dismissPresentation(); 
    } 
} 

/** 
* Utility method to allow the user to change the cube color. 
*/ 
public void changeColor() { 
    mCubeRenderer.changeColor(); 
} 

}

+1

[NullPointerException은 무엇이며 어떻게 수정합니까?] (http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and) -how-do-i-fix-it) – Sanoop

+0

예외는'CubeRenderer' 클래스에서 발생하지 않습니다. 'null ','CubeRenderer' 참조로'CubeRenderer' 클래스의'changeColor()'메소드에 접근하려 할 때 발생합니다. 따라서, 그 메소드를 호출하는 코드와 'CubeRenderer' 인스턴스를 인스턴스화하거나 유효한 참조를 얻는 것으로 간주되는 코드를 보여 주어야합니다. 왜냐하면 문제가있는 곳이기 때문입니다. –

+0

감사합니다 @MarkusKauppinen – user3563954

답변

0

문제가 해결 처리해야 정확한 장소 mCubeRenderer = 새로운 CubeRe nderer(); 이 라인을 PresentationService 클래스에 추가해야합니다.