-1

I have my main Activity in which I have a few Buttons.
One of them is supposed to open the Camera.
To make it cleaner I am using View.onClick(), so I will just have the Button on the main Activity and the rest will be managed by another other class (Camactivity).

In my main Activity :

  Button btnrep = (Button)findViewById(R.id.button3);
  btnrep.setOnClickListener(new Camactivity(this));

and in my

public class Camactivity extends Activity implements View.OnClickListener

    private File imageFile;
    private Context appContext;

    public MainActivity_1(Context context)
    {
        appContext = context;
    }


    @Override
    public void onClick(View view) {

                Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);//use intent and pass in mediastore
                //mediastore is a databases where image and video are stores and link

                imageFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "BreedingGround.bmp");
        /*link to a directory - pass in directory where you want to save the pictures and names of the file*/


                Uri tempuri = Uri.fromFile(imageFile);//Convert imageFile to a Uri
                intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, tempuri);//location where u want the image file to be save after taking photo
                intent.putExtra(android.provider.MediaStore.EXTRA_VIDEO_QUALITY, 1);//quality of out image, 1 means high quality image


                startActivityForResult(intent, 0);//Request code 0 to identify who send the request

    }

However I am getting a null pointer error at startActivityForResult(intent, 0);

 FATAL EXCEPTION: main Process: com.example.mohit.softeng, PID: 22075
  java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference
      at android.content.ContextWrapper.getPackageManager(ContextWrapper.java:97)
      at com.example.mohit.softeng.MainActivity_1.onClick(MainActivity_1.java:60)
      at android.view.View.performClick(View.java:5697)
      at android.widget.TextView.performClick(TextView.java:10826)
      at android.view.View$PerformClick.run(View.java:22526)
      at android.os.Handler.handleCallback(Handler.java:739)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:158)
      at android.app.ActivityThread.main(ActivityThread.java:7225)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
ace7410
  • 1
  • 2
  • 1
    1) Never `new` an Activity class. 2) Activity class should not have constructors. 3) You do not need to pass a Context to an Activity since `Activity extends Context` – OneCricketeer Oct 28 '16 at 17:06
  • For the 2 and 3 suggestion i am able to edit however for the ( new ) i am unable to remove as if i just put my activity name in btnrep.setOnClickListener(Camactivity()); i am receiving an error to which only suggestion is to put new – ace7410 Oct 28 '16 at 17:13
  • You don't need a separate activity - that was my main point. Plus, never "construct" an Activity outside of `startActivity` was my other point. – OneCricketeer Oct 28 '16 at 17:14

1 Answers1

0

You are more than welcome to define a View.OnClickListener subclass, but there's really no reason I see to have Camactivity at all, so just implement View.OnClickListener onto MainActivity.

Besides, you will likely want the result from startActivityForResult within MainActivity's onActivityResult, not Camactivity.

So, with that suggestion

Button btnrep = (Button)findViewById(R.id.button3);
btnrep.setOnClickListener(this);

If you already have your Activity implementing an OnClickListener, then add an if-statement to check which button is clicked.

@Override
public void onClick(View view) {
    switch (v.getId()) {
        case R.id.button3:
            openCamera();
    }
}

private void openCamera() {
    // That other code in Camactivity
}


Alternative answer... still not using new to make an Activity class.

Button btnrep = (Button)findViewById(R.id.button3);
btnrep.setOnClickListener(new View.onClickListener() {
    @Override
    public void onClick(View v) {
        Intent cam = new Intent(MainActivity.this, Camactivity.class);
        startActivityForResult(cam, 0); // If you need the result in MainAcivity, pass it back from camActivity
        // else, just startActivity(cam);
    }
});

Then, update Camactivity to start the camera intent as soon as it is created.

public class Camactivity extends Activity {

    private File imageFile;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);//use intent and pass in mediastore
        //mediastore is a databases where image and video are stores and link

        imageFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "BreedingGround.bmp");
/*link to a directory - pass in directory where you want to save the pictures and names of the file*/

        Uri tempuri = Uri.fromFile(imageFile);//Convert imageFile to a Uri
        intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, tempuri);//location where u want the image file to be save after taking photo
        intent.putExtra(android.provider.MediaStore.EXTRA_VIDEO_QUALITY, 1);//quality of out image, 1 means high quality image

        startActivityForResult(intent, 0);//Request code 0 to identify who send the request

    } 

    @Override
    public void onActivityResult (int requestCode, int resultCode, Intent data) {
        if (requestCode == 0) {
            // TODO: Handle camera intent result
        }
    }



}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • i am able to run the application if i have everything under main activity , as per you suggested, however i have to separate the UI(button) and the logic(Camactivity) therefore i am trying to do it this way – ace7410 Oct 28 '16 at 17:23
  • Okay... not sure who is enforcing that requirement on you. I'll edit, but I don't like the solution. – OneCricketeer Oct 28 '16 at 17:27
  • Sorry about the trouble !! but apparently it makes whole application flow more "visible" so we have to follow. – ace7410 Oct 28 '16 at 17:36
  • Sorry there are no errors after following the alternative answer but upon clicking the button it is just showing a white screen thats all. – ace7410 Oct 28 '16 at 17:49
  • Well, it doesn't need `setContentView`, so yeah, it'll be a blank activity. It should, however, start the camera intent as soon as the activity starts. – OneCricketeer Oct 28 '16 at 17:53
  • Oh sorry about that it was an error on my side. thank you very much for your help. the camera works! – ace7410 Oct 28 '16 at 18:04