2014-03-14 37 views
0

我試圖把我的一個XML佈局GLSurfaceView與其他UI元素一起,並不斷收到錯誤充氣類com.vi.cubo01.MyGLSurfaceView在logcat中。錯誤充氣GLSurfaceView

下面是Java代碼:

super.onCreate(savedInstanceState); 

      mGLSurfaceView = (MyGLSurfaceView) findViewById(R.id.vistaGLSuperficie) 
      setContentView(R.layout.activity_main);    
    } 

    @Override 
    protected void onResume() 
    { 

      super.onResume(); 
      mGLSurfaceView = (MyGLSurfaceView) findViewById(R.id.vistaGLSuperficie); 
      mGLSurfaceView.onResume(); 
    } 

    @Override 
    protected void onPause() 
    { 
      super.onPause(); 
      mGLSurfaceView = (MyGLSurfaceView) findViewById(R.id.vistaGLSuperficie); 
      mGLSurfaceView.onPause(); 
    } 



    class MyGLSurfaceView extends GLSurfaceView { 
     public MyGLSurfaceView(Context context) { 
      super(context); 
      setEGLContextClientVersion(2); 
      setRenderer(new CustomRenderer()); 
     } 
    } 

和XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<EditText 
    android:id="@+id/editText1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" > 

    <requestFocus /> 
</EditText> 

<EditText 
    android:id="@+id/editText2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:inputType="textMultiLine" /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="0" /> 

<SeekBar 
    android:id="@+id/seekBar1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

<com.vi.cubo01.MyGLSurfaceView 
    android:id="@+id/vistaGLSuperficie" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    /> 

回答

3

如果從XML膨脹,則需要包括(上下文,AttributeSet中)構造函數除了你已經擁有的(Context)構造函數之外。這是因爲這是佈局充氣機爲了處理您在XML中指定的屬性而需要調用的那個,例如layout_width和layout_height。

public MyGLSurfaceView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    setEGLContextClientVersion(2); 
    setRenderer(new CustomRenderer()); 
} 
+0

對不起,錯誤依然存在,可能來自XML嗎? – user3185164

+0

從您發佈的代碼中,不清楚MyGLSurfaceView是否在您的Activity類中定義 - 確保您已將其定義在它之外。同樣在你的onCreate()中,確保你只在調用setContentView之後使用findViewById,而不是之前,否則你會得到null。 – NigelK

+0

謝謝,只要我從主要活動中刪除MyGLSurfaceView,問題就解決了。 – user3185164