0

所以我实现了一个TextureView直通代码。但是现在没有显示按钮,并且 onclick 侦听器给了我一个空指针异常。我究竟做错了什么

  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>
4

1 回答 1

1

您应该TextureViewactivity_camera.xml. 您应该使用 findViewById检索activity_camera.xml( ) 的根并调用您获得的实例。调用将简单地替换视图层次结构。RelativeLayoutaddView(mTextureView)RelativeLayoutsetContentView(mTextureView)

于 2013-10-15T09:11:17.357 回答