我知道此错误消息已有答案,但这些解决方案都没有解决我的问题。我正在尝试Sierpinski Triangle
,我有一个MySurfaceView
类扩展GLSurfaceView
和MyRenderer
类扩展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;
}
}
答案 0 :(得分:1)
在活动的onCreate()
view = new MySurfaceView(this, renderer);
您创建了MySurfaceView
类的实例。它的构造函数(MySurfaceView(Context context, MyRenderer renderer)
)在行
renderer
this.setRenderer(renderer);
因此,当您尝试在活动的onCreate()
view.setRenderer(renderer);
你得到了错误。
这条线是多余的。