-2

I am trying to have multiple buttons on my camera preview. The app has worked with a record button and a drop-down menu button. However, once I tried to add zoom buttons for the reticle, the app crashes. The code compiles but the app won't work. Can't seem to find a helpful answer anywhere else.

I have been maneuvering lines of code between onViewCreated(), onClick(), and onCreate() but I can't get the app to run.

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);

    if (null == savedInstanceState) {
        getFragmentManager().beginTransaction()
            .replace(R.id.container,Camera2VideoFragment.newInstance())
            .commit();
    }
    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}

@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
    mTextureView = view.findViewById(R.id.texture);
    mButtonVideo = view.findViewById(R.id.video);
    mZoomIn = view.findViewById(R.id.imageButton1);
    mZoomOut = view.findViewById(R.id.imageButton2);

    mButtonVideo.setOnClickListener(this);
    mZoomIn.setOnClickListener(this);
    mZoomOut.setOnClickListener(this);

    view.findViewById(R.id.info).setOnClickListener(this);
    view.findViewById(R.id.imageView1).setOnClickListener(this);
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.video: {
            if (mIsRecordingVideo) {
                stopRecordingVideo();
            } else {
                startRecordingVideo();
            }
            break;
        }
        case R.id.info: {
            Activity activity = getActivity();
            if (null != activity) {
                new AlertDialog.Builder(activity)
                        .setMessage(R.string.intro_message)
                        .setPositiveButton(android.R.string.ok, null)
                        .show();
            }
            break;
        }

        case R.id.imageButton1: {
            Activity activity = getActivity();
            if (null != activity) {
                ImageView img = view.findViewById(R.id.imageView1);
                float x = img.getScaleX();
                float y = img.getScaleY();

                img.setScaleX(x + 2);
                img.setScaleY(y + 2);
            }
        }
    }
}

The code compiles but the app crashes. Logcat tells me the error is in the onViewCreated() with the setOnClickListeners(). The findViewById() is assigning null to the buttons but I can't figure out why.

I eventually want to make the zoom in/out buttons to zoom the video preview instead of the reticle. Any help with that would be great too. I also plan to make an adjustable brightness seekbar so help with that would be good too.

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • I've read many posts, including "What is a NullPointerException, and how do I fix it?" I understand the logic behind it but I can't seem to figure it out for my code specifically. – Dimarnachos Jun 11 '19 at 17:26
  • This might help you investigate the problem: https://developer.android.com/reference/android/app/Activity#findViewById(int) – Maxime Launois Jun 11 '19 at 17:37

1 Answers1

0

I can't understand why you use onViewCreated() you may implement onCreateView(...) like following code:

@Override
  protected void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_camera);

     if (null == savedInstanceState) {
    getFragmentManager().beginTransaction()
        .replace(R.id.container,Camera2VideoFragment.newInstance())
        .commit();
}




getWindow().getDecorView()
.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}

@Override
 public void onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
 View view = inflater.inflate(R.layout.YOUR_LAYOUT, container, false);
 mTextureView = view.findViewById(R.id.texture);
 mButtonVideo = view.findViewById(R.id.video);
 mZoomIn = view.findViewById(R.id.imageButton1);
 mZoomOut = view.findViewById(R.id.imageButton2);

mButtonVideo.setOnClickListener(this);
mZoomIn.setOnClickListener(this);
mZoomOut.setOnClickListener(this);

view.findViewById(R.id.info).setOnClickListener(this);
view.findViewById(R.id.imageView1).setOnClickListener(this);
return view;
}

     @Override
   public void onClick(View view) {
   switch (view.getId()) {
    case R.id.video: {
        if (mIsRecordingVideo) {
            stopRecordingVideo();
        } else {
            startRecordingVideo();
        }
        break;
    }
    case R.id.info: {
        Activity activity = getActivity();
        if (null != activity) {
            new AlertDialog.Builder(activity)
                    .setMessage(R.string.intro_message)
                    .setPositiveButton(android.R.string.ok, null)
                    .show();
        }
        break;
    }

    case R.id.imageButton1: {
        Activity activity = getActivity();
        if (null != activity) {
            ImageView img = view.findViewById(R.id.imageView1);
            float x = img.getScaleX();
            float y = img.getScaleY();

            img.setScaleX(x + 2);
            img.setScaleY(y + 2);
         }
      }
  }
  }
shahram_javadi
  • 257
  • 1
  • 12