2012-01-13 65 views
2

我有一個活動,我已將活動的內容視圖設置爲「R.layout.main.xml」。而且我有另一個包含使用openGL創建的動畫的類。現在我需要在活動的背景中使用此動畫。在活動中使用GlSurfaceview

的代碼是這樣的

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.main_pixie); 

    mGLView = new ClearGLSurfaceView(this); 
    setContentView(mGLView); 
} 

但我的應用程序崩潰..How我可以解決這個問題。

回答

3

當您第二次撥打setContentView()時,您將替換第一次設置的內容,只留下背景。崩潰很可能是因爲您依賴主佈局中的元素,而這些元素已被刪除。

而不是打電話setContentView()兩次,你應該在主佈局中包括GLSurfaceView。下面是如何可以做到這一點的例子:

<?xml version="1.0" encoding="UTF-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent> 
    <your.application.package.ClearGLSurfaceView 
     android:layout_width="match_parent" 
     android:layout_width="match_parent"/> 
    <!--put the rest of your layout here, i.e the contents of the original R.layout.main_pixie--> 
</FrameLayout> 

然後你就可以在你的onCreate()如常(main_pixie_new指的是上面的XML加載這個佈局,我只是把它這個名字讓事情儘可能明確):

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.main_pixie_new); 
}