2013-11-01 56 views
3

我想把一個GLSurfaceView和一個按鈕放到同一個xml佈局中。但是我的應用程序在我的設備上運行時會自動關閉。 是否有人可以幫助我,告訴我錯過了什麼或者我的代碼出了什麼問題?XML格式的Android 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" 
     android:orientation="vertical" 
     android:id= "@+id/linearlayout1" > 

    <Button 
     android:id="@+id/buttonID" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dip" 
     android:text="A Button" /> 

    <com.example.test2.MyGLSurfaceView 
     android:id="@+id/glSurfaceViewID" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.23" /> 

</LinearLayout> 

============ ====

package com.example.test2; 

import android.opengl.GLSurfaceView; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.view.Menu; 
import android.view.MotionEvent; 

public class MainActivity extends Activity { 

    private GLSurfaceView mGLView; 

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

     // Create a GLSurfaceView instance and set it 
     // as the ContentView for this Activity 
     mGLView = new MyGLSurfaceView(this); 
     //setContentView(mGLView); 
     setContentView(R.layout.activity_main); 
    } 

// @Override 
    protected void onPause() { 
     super.onPause(); 
     mGLView = (MyGLSurfaceView)findViewById(R.id.glSurfaceViewID); 
     mGLView.onPause(); 
    } 
// 
    @Override 
    protected void onResume() { 
     super.onResume(); 
     mGLView = (MyGLSurfaceView)findViewById(R.id.glSurfaceViewID); 
     mGLView.onResume(); 
    } 
} 

class MyGLSurfaceView extends GLSurfaceView { 

    private final MyGLRenderer mRenderer; 

    public MyGLSurfaceView(Context context) { 
     super(context); 

     // Create an OpenGL ES 2.0 context. 
     setEGLContextClientVersion(2); 

     // Set the Renderer for drawing on the GLSurfaceView 
     mRenderer = new MyGLRenderer(); 
     setRenderer(mRenderer); 

     // Render the view only when there is a change in the drawing data 
     setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); 
    } 

    private final float TOUCH_SCALE_FACTOR = 180.0f/320; 
    private float mPreviousX; 
    private float mPreviousY; 

    @Override 
    public boolean onTouchEvent(MotionEvent e) { 
     // MotionEvent reports input details from the touch screen 
     // and other input controls. In this case, you are only 
     // interested in events where the touch position changed. 

     float x = e.getX(); 
     float y = e.getY(); 

     switch (e.getAction()) { 
      case MotionEvent.ACTION_MOVE: 

       float dx = x - mPreviousX; 
       float dy = y - mPreviousY; 

       // reverse direction of rotation above the mid-line 
       if (y > getHeight()/2) { 
        dx = dx * -1 ; 
       } 

       // reverse direction of rotation to left of the mid-line 
       if (x < getWidth()/2) { 
        dy = dy * -1 ; 
       } 

       mRenderer.mAngle += (dx + dy) * TOUCH_SCALE_FACTOR; // = 180.0f/320 
       requestRender(); 
     } 

     mPreviousX = x; 
     mPreviousY = y; 
     return true; 
    } 

} 

===========================

謝謝。

回答

5

在MainActivity.onCreate()中使用findViewById而不是創建一個新的View(就像在onPause,onResume中做的那樣)並且只分配一次該變量。

setContentView(R.layout.activity_main); 
mGLView = (MyGLSurfaceView) findViewById(R.id.glSurfaceViewID); 

確保MyGLSurfaceView具有AttributeSet的構造函數,以便它可以從XML中充氣。查看構造函數的stackoverflow atricle

而且還改變了XML中Button和MyGLSurfaceView的順序,因爲現在Button將是以下的佈局中的MyGLSurfaceView,所以你將無法看到它。

+0

非常感謝您的建議。有用!好極了! =)。我很感激 。 – AuroraBlaze