已为此实例调用setRenderer

时间:2014-04-13 21:53:26

标签: java android opengl-es rendering glsurfaceview

我知道此错误消息已有答案,但这些解决方案都没有解决我的问题。我正在尝试Sierpinski Triangle,我有一个MySurfaceView类扩展GLSurfaceViewMyRenderer类扩展Activity并实现Renderer }。以下是课程:

 import android.content.Context;
 import android.opengl.GLSurfaceView;
 import android.view.MotionEvent;

 public class MySurfaceView extends GLSurfaceView {

private MyRenderer renderer;
private float prevX; 
private float prevY;
private float ROTATION_FACTOR = 1.0f;


public MySurfaceView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

public MySurfaceView(Context context, MyRenderer renderer) {
    super(context);
    this.renderer = renderer;
    this.setRenderer(renderer);
    this.setRenderMode(RENDERMODE_WHEN_DIRTY);          
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    switch(event.getAction()){
    case MotionEvent.ACTION_MOVE:
        float x = event.getX();
        float y = event.getY(); 
        float dx = x-prevX;
        float dy = y-prevY;
        prevX = x;
        prevY = y;
        float distance = (float) Math.sqrt((double) (dx*dx+dy*dy));
        float angle = distance*ROTATION_FACTOR;
        if(dy>0){
            renderer.increaseRotationAngle(angle);
        }else{
            renderer.increaseRotationAngle(-angle);
        }
        this.requestRender();
       }
       return true;
    }

}


 import javax.microedition.khronos.egl.EGLConfig;
 import javax.microedition.khronos.opengles.GL10;

 import android.app.Activity;
 import android.opengl.GLSurfaceView.Renderer;

 public class MyRenderer extends Activity implements Renderer {

private Triangle mTriangle;
private float rotationAngle = 0;
private int level;
private int mFill;

public MyRenderer(int level, int mFill)
{
    this.level = level;
    this.mFill = mFill;
    mTriangle = new Triangle(5, "WireFrame");
}

@Override
public void onDrawFrame(GL10 gl) {
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);
    gl.glLoadIdentity();
    gl.glRotatef(rotationAngle, 0, 0, 1);
    mTriangle.onDrawFrame(gl);
    // TODO Auto-generated method stub
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {

    int delta;
    if(width<height){
    delta=(height-width)/2;
    gl.glViewport(0, delta, width, width);
    }else{
    delta=(width-height)/2;
    gl.glViewport(delta, 0, height, height);
    }
    gl.glMatrixMode(GL10.GL_PROJECTION); 
    gl.glLoadIdentity();
    gl.glOrthof(-1.0f, 1.0f, -1.0f, 1.0f, -0.1f, 0.1f);
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    // TODO Auto-generated method stub
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {

    gl.glClearColor(0.8f, 0.8f, 0.8f, 1);
    // TODO Auto-generated method stub
}

public void increaseRotationAngle(float delta)
{
    rotationAngle+=delta;
    System.out.println("Rotating by "+rotationAngle);
}
}


 import android.os.Bundle;
 import android.app.Activity;
 import android.view.Menu;

 public class MainActivity extends Activity {

private MySurfaceView view;
private MyRenderer renderer;
private String levelCount;
public String skew;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);

    renderer = new MyRenderer(3, 1);
    view = new MySurfaceView(this, renderer);
    view.setRenderer(renderer);
    setContentView(view);

}

@Override
public void onPause()
{
    view.onPause();
    super.onPause();
}

@Override
public void onResume()
{
    view.onResume();
    super.onResume();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
 }

1 个答案:

答案 0 :(得分:1)

活动onCreate()

的以下行中
view = new MySurfaceView(this, renderer);

您创建了MySurfaceView类的实例。它的构造函数(MySurfaceView(Context context, MyRenderer renderer))在行

中设置renderer
this.setRenderer(renderer);

因此,当您尝试在活动onCreate()

的下一行设置渲染器时
view.setRenderer(renderer);

你得到了错误。

这条线是多余的。