投射远程显示android Nullpointer

时间:2016-09-21 07:20:08

标签: android chromecast

当我们在CubeRender类中单击更改颜色按钮nullpointer异常时。

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

java class

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 个答案:

答案 0 :(得分:0)

问题解决了mCubeRenderer = new CubeRenderer();需要在PresentationService

中添加此行