Mayank Thakur
Mayank Thakur

Reputation: 61

Calling a non static method from a in a static context

im sorry for the long question, but i could really use the help

so I've been trying to make a camera app for this school project that i have. i'm really new to coding in general, and i don't really know much about Java. i decided to use the CameraKit library by Furgle to help me with this. they say all i have to do is include

 protected void onResume() {
    super.onResume();
    CameraView.start();

and

 @Override
    protected void onPause () {
        super.onPause();
        CameraView.stop();

    }

i should be able to start and stop the camera preview im trying to create. however, when i added this code to my main activity, i got the following:

non static method 'stop()' / 'start()' cannot be referenced from a static context

I've tried a few things like trying to create an object of the class and calling the method from that (i'm not completely sure if i said that right or not)

   @Override
protected void onResume() {
    super.onResume();
    CameraView main = new CameraView()
    main.start();

when i try to run that, i get:

cannot resolve constructor CameraView()

I also tried to create instances of the class called "CameraView" which is where the method "start();" and "stop();" are. sadly i have not been able to get anywhere with that.

the point is i tried everything that i could understand but any help would be greatly appreciated.

after looking into the code for the library, i saw that neither the start method or the stop method within the CameraView class are declared "static". so i really don't see where the problem is coming from and how to overcome it

Upvotes: 0

Views: 111

Answers (1)

Ben P.
Ben P.

Reputation: 54204

Assuming the tutorial you're following is this one https://github.com/gogopop/CameraKit-Android#usage ...

When they say that "all you have to do" is add this code:

@Override
protected void onResume() {
    super.onResume();
    cameraView.start();
}

@Override
protected void onPause() {
    cameraView.stop();
    super.onPause();
}

They're speaking to more-experienced developers. The part they're leaving out is where does cameraView come from?

Well, the first step is to include a <CameraView> in your layout. But even after that, you need to find it and assign it to a cameraView variable. So really, you need all this:

public class MainActivity extends AppCompatActivity {

    private CameraView cameraView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); // `activity_main.xml` must have a `<CameraView>` tag with id `camera`
        cameraView = (CameraView) findViewById(R.id.camera);
    }

    @Override
    protected void onResume() {
        super.onResume();
        cameraView.start();
    }

    @Override
    protected void onPause() {
        cameraView.stop();
        super.onPause();
    }
}

Upvotes: 3

Related Questions