我是Android上的OpenGL的新手,上週我描述了一個問題,無法讓渲染器在佈局中聲明的GLSurfaceView啓動。如果我在Activity類和setContentView中聲明渲染器,它會開始很好。這裏有一個簡化版,全部爲的源代碼。我究竟做錯了什麼?試圖從佈局中聲明的GLSurfaceView開始渲染器
佈局。 。 。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/dummy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="a Button" />
<FrameLayout
android:id="@+id/framelay"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.test.rendertest.RTSurface
android:id="@+id/RTSurfaceView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</FrameLayout>
</LinearLayout>
Activity類。取消註釋// A's,並註釋掉// B並且渲染器運行。但是,如下所示,即使構造函數被調用,渲染器也不會運行。
public class RenderTest extends Activity {
RTSurface myView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// myView = new RTSurface(this); // A
// setContentView(myView); //A
setContentView(R.layout.main); // B
myView = (com.test.rendertest.RTSurface)findViewById(R.id.RTSurfaceView); //B
}
@Override
protected void onPause() {
super.onPause();
myView.onPause();
}
@Override
protected void onResume() {
super.onResume();
myView.onResume();
}
}
GLSurfaceView。 。 。
class RTSurface extends GLSurfaceView {
private final RTRenderer renderer;
public RTSurface(Context context) {
super(context);
Log.i("rendertest", "RTSurface constructor - Default Form");
renderer = new RTRenderer();
setRenderer(renderer);
}
public RTSurface(Context context, AttributeSet attrs) {
super(context, attrs);
Log.i("rendertest", "RTSurface constructor - Layout Form");
renderer = new RTRenderer();
setRenderer(renderer);
}
}
。 。 。和渲染(只是存根)
class RTRenderer implements GLSurfaceView.Renderer {
public RTRenderer() {
// a constructor just to have somewhere to set
// breakpoints and logcat messages
Log.i("rendertest", "RTRenderer Constructor");
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
Log.i("rendertest", "onSurfaceCreated in RTRenderer");
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
Log.i("rendertest", "onSurfaceChanged in RTRenderer");
}
public void onDrawFrame(GL10 gl) {
Log.i("rendertest", "onDrawFrame in RTRenderer");
}
}
在此先感謝!
我複製並粘貼代碼到項目,一切對我的作品...... – Jave 2011-12-18 22:08:11
@Jave:怎麼可能呢? LadaRaider(以下)正確地確定了(非常真實的)問題。 – 2011-12-19 00:59:35
哈哈,當然!我只複製了Java代碼並編寫了自己的佈局文件,因爲我不認爲這會是錯誤。現在看起來很明顯,我讀了它。 – Jave 2011-12-19 08:55:36