2013-11-01 3 views
3

동일한 XML 레이아웃에 GLSurfaceView와 버튼을 넣으려고합니다. 하지만 내 응용 프로그램은 장치에서 실행할 때마다 자동으로 닫힙니다. 누군가 나를 도우 려하고 내가 무엇을 놓치고 있는지 또는 내 코드에 무엇이 잘못된지 말할 수 있습니까? 여기 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()에서 새보기 (예 : onPause, onResume에서와 같이)를 만드는 대신 findViewById를 사용하고 변수를 한 번만 할당합니다.

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

MyGLSurfaceView에 AttributeSet이있는 생성자가 있는지 확인하여 XML에서 비정상적으로 확장 될 수 있습니다. 생성자에 대해서는 stackoverflow atricle을 확인하십시오. 당신이 그것을 볼 수 없습니다, 그래서 지금 버튼 레이아웃에 MyGLSurfaceView 아래 될 것이기 때문에

그리고

는 XML의 버튼과 MyGLSurfaceView의 순서를 변경 aswell.

+0

감사합니다. 그것은 작동합니다! 예! =). 감사합니다 . – AuroraBlaze

관련 문제