Search code examples
javaandroidchronometer

How can I call a method from another class?


I can't figure out how to call the start(); method from a class I made It is in the same package and it is called Chronometer here is me class

    import java.lang.reflect.Method;
    import java.text.DecimalFormat;
    import com.example.chrono.Chronometer.OnChronometerTickListener;
    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.os.SystemClock;
    import android.util.AttributeSet;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

    public class tests extends Activity{
    Button start;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Chronometer test = new Chronometer(this);
        start = (Button) findViewById(R.id.bStart);

        start.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                com.example.chrono.Chronometer.start();         } 
        }); 
    }

}

i get the error on com.example.chrono.Chronometer.start();

the error is Cannot make a static reference to the non-static method start() from the type Chronometer


Solution

  • You can Call non static method's from other class as using object of Chronometer class:

    Chronometer test = new Chronometer(this);
        start = (Button) findViewById(R.id.bStart);
    
        start.setOnClickListener(new View.OnClickListener() {
    
            public void onClick(View v) {
            //    com.example.chrono.Chronometer.start();
                test.start(); 
             } 
        });