Android call cameraApp from inactive class related issues
Although I found several similar answers to questions, I could not solve my problem:
I am using Fragment oFragment
where I want to call the camera as a new activity. So I created an inactive Camera class. On startup, I get the error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.app.Activity.getPackageManager()' on a null object reference
oFragment
placed in FrameLayout
in MainActivity
. I also tried the constructor Camera
with Context context
. Do you see any workarounds or bugs? Thanks in advance!
Edit: The code works great if implemented directly in oFragment
.
Camera class:
public class Camera {
static final int REQUEST_IMAGE_CAPTURE = 1;
Activity activity;
public Camera(Activity activity) {
this.activity = activity;
}
public void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(activity.getPackageManager()) != null) {
activity.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
Taken from a fragment:
public class OFragment extends Fragment {
Camera cam = new Camera(getActivity());
...
@Override
public void onClick(View v) {
cam.dispatchTakePictureIntent();
}
source to share