2016-12-31 12 views
1

我對OpenGL相當陌生,我的問題是當我做SetContentView("R.layout.main")沒有任何反應,但是當我做SetContentView("mGLView")時,遊戲顯示正常。我的main.xml文件包含一個表面視圖,因此我會認爲它會顯示,因爲main.xml有一個表面視圖。OpenGL ES 2:當SetContentView設置爲XML文件時,GLSurface視圖顯示黑色屏幕

我懷疑一種方法來解決這個問題,將與addview或findviewbyid完成,但我的意思是失敗。

培訓相關MainActivity代碼:

public class OpenGLES20Activity extends Activity { 
private GLSurfaceView mGLView; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    mGLView = new MyGLSurfaceView(this); 
    setContentView(R.layout.main); 

的main.xml代碼:

<FrameLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <RelativeLayout android:id="@+id/linearLayout1" android:layout_width="wrap_content" 
android:layout_height="wrap_content"> 

      <SurfaceView 
android:id="@+id/SurfaceView" 
android:layout_width="500dp" 
android:layout_height="478dp"> 
</SurfaceView> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/Timer" 
       android:id="@+id/Timer"/> 
     </RelativeLayout> 

    </FrameLayout> 

任何幫助表示讚賞。

回答

0

您需要設置擴展GLSurfaceView而不是僅僅SurfaceView在XML文件中,這樣只需改變SurfaceViewcom.example.yourapp.MyGLSurfaceView

<com.example.yourapp.MyGLSurfaceView 
    android:id="@+id/SurfaceView" 
    android:layout_width="500dp" 
    android:layout_height="478dp"/> 
+0

我所做的變化在我的代碼,現在我有以下的錯誤錯誤:(31,19)錯誤:類MyGLSurfaceView中的構造函數MyGLSurfaceView無法應用於給定的類型; 必需:上下文,AttributeSet 找到:沒有參數 原因:實際和正式參數列表長度不同 – DarkEnergy

+0

您的構造函數需要爲'MyGLSurfaceView(Context context,AttributeSet attrs)''就像我已鏈接到您的文檔中所示。 – MarGenDo

+0

我的構造函數被設置爲,我仍然得到錯誤。 如果這有助於我的IDE已經表明這是一個問題:mGLView = new MyGLSurfaceView(this); – DarkEnergy