2013-10-15 8 views
0

그래서 코드를 통해 TextureView을 구현했습니다. 이제는 버튼이 표시되지 않고 onclick 리스너가 null 포인터 예외를 제공합니다. 어떻게 내가 잘못TextureView가 구현 될 때 버튼이 표시되지 않습니다.

protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_camera); 

     mTextureView = new TextureView(this); 
     mTextureView.setSurfaceTextureListener(this); 
     setContentView(mTextureView); 

     if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){ 
      Toast.makeText(this, "No camera on device", Toast.LENGTH_LONG).show(); 
     }else { 
      cameraId = findFrontFacingCamera(); 
      if (cameraId<0){ 
       Toast.makeText(this, "no front camera", Toast.LENGTH_LONG).show(); 

      }else{ 
       camera = Camera.open(cameraId); 
       Toast.makeText(this, "We have camera", Toast.LENGTH_LONG).show(); 
      } 

     } 

     findViewById(R.id.buttonMenu).setOnClickListener(
       new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         // No account, load new account view 
         Intent intent = new Intent(CameraActivity.this, 
          MenuActivity.class); 
         startActivityForResult(intent, 0); 
        } 
       }); 

    } 


    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, 
      int height) { 
     camera = Camera.open(); 

     Camera.Size previewSize = camera.getParameters().getPreviewSize(); 
     mTextureView.setLayoutParams(new FrameLayout.LayoutParams(
       30, 30, Gravity.CENTER)); 

     try { 
      camera.setPreviewTexture(surface); 
     } catch (IOException t) { 
     } 

     camera.startPreview(); 

     mTextureView.setAlpha(0.8f); 
     mTextureView.setRotation(45.0f); 

    } 

    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, 
      int height) { 
     // Ignored, the Camera does all the work for us 
    } 


    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { 
     camera.stopPreview(); 
     camera.release(); 
     return true; 
    } 


    public void onSurfaceTextureUpdated(SurfaceTexture surface) { 
     // Called whenever a new frame is available and displayed in the 

     rotation += 1.0f; 
     if (rotation > 360) { 
      rotation = 0; 
     } 
     mTextureView.setRotation(rotation); 
    } 


     @Override 
     protected void onPause() { 
     if (camera != null) { 
      camera.release(); 
      camera = null; 
     } 
     super.onPause(); 
     } 

     public void onClick(View view) { 
      if (camera == null){ 
       Toast.makeText(this, "Camera is null", Toast.LENGTH_LONG).show(); 

      }else{ 

      camera.takePicture(null, null, 
       new PhotoHandler(getApplicationContext(), mPreferences)); 

      } 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.camera, menu); 
     return true; 
    } 

} 

XML 파일을하고있는 중이 야 :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".CameraActivity" > 



    <Button 
     android:id="@+id/buttonMenu" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/capture" 
     android:layout_alignBottom="@+id/capture" 
     android:layout_alignParentLeft="true" 
     android:layout_marginLeft="22dp" 
     android:text="menu" /> 

    <Button 
     android:id="@+id/capture" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="24dp" 
     android:onClick="onClick" 
     android:text="Take pic" /> 

</RelativeLayout> 
+1

'setContentView (mTextureView); '로 전체 뷰를 변경하기 때문에'setContentView (R.layout.activity_camera);'로 추가 된 UI를 볼 수 없기 때문에. –

답변

1

당신은 activity_camera.xml 코드를 통해 TextureView가 만든 추가해야합니다. findViewById를 사용하여 activity_camera.xml (RelativeLayout)의 루트를 검색하고 RelativeLayout 인스턴스에서 addView(mTextureView)을 호출해야합니다. setContentView(mTextureView)을 호출하면 뷰 계층 구조가 간단히 바뀝니다.