已为此实例调用setRenderer

问题描述 投票:1回答:1

我知道这个错误消息已有答案,但这些解决方案都没有帮我解决问题。我正在尝试做一个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;
}
 }
java android opengl-es rendering glsurfaceview
1个回答
1
投票

在活动的onCreate()的以下行

view = new MySurfaceView(this, renderer);

你创建了一个MySurfaceView类的实例。它的构造函数(MySurfaceView(Context context, MyRenderer renderer))在行中设置了renderer

this.setRenderer(renderer);

所以,当你试图在你的Activity的onCreate()的下一行设置渲染器时

view.setRenderer(renderer);

你得到错误。

这条线是多余的。

© www.soinside.com 2019 - 2024. All rights reserved.