Question

So I implemented a TextureView through code. Now the buttons however are not being shown, and the onclick listener is giving me a null pointer exception. What am I doing wrong

  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 file:

<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>
Was it helpful?

Solution

you should add the TextureView you created through code in the activity_camera.xml. You should retrieve the root for activity_camera.xml (RelativeLayout) with findViewById and call addView(mTextureView) on the RelativeLayout instance you get. Calling setContentView(mTextureView) will simply replace the views hierarchy.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top