如何从Android中的另一个类调用方法?

时间:2010-12-11 13:40:40

标签: android

我一直试图从另一个类调用一个方法。

我有2节课。 Timer.class和DecisionMaking.class。一旦定时器为onFinish(),Timer类就会调用DecisionMaking.class中的方法(我现在只使用toast)。我的代码没有错误,但是当计时器完成计数时,方法未被调用,模拟器要求强制关闭。这两个类都在同一个包中。有什么我想念的吗?

Timer.class

public class Timer extends Activity{

  TextView timeDisplay;
  MyCount timer;
  int length = 10000;
  long startTime;
  long timeRemaining;
  DecisionMaking decisionmaking;


  /** Called when the activity is first created. */

  public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

     timeDisplay = (TextView) findViewById(R.id.timer);
     timer = new MyCount(length, 1000);

     timer.start();

 } 

public class MyCount extends CountDownTimer {

public MyCount(long millisInFuture, long countDownInterval) {
           super(millisInFuture, countDownInterval);
    }

    public void onFinish() {

      DecisionMaking decisionmaking = new DecisionMaking(); 
      decisionmaking.sendSms();

     timer.start();
    }

    public void onTick(long millisUntilFinished) {
       timeDisplay.setText("Time:" + millisUntilFinished / 1000);
    }
  }

}

DecisionMaking.class

public class DecisionMaking extends Timer{  

 public void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState);  

    sendSms();
    }

    public boolean sendSms() {

     Calendar cal = Calendar.getInstance();
         Date d = cal.getTime();
         int hour = d.getHours();

     if (hour > 0 && hour < 6)
        return false;
     else
        {
        //Call SMS class here, remove toast
        Toast.makeText(getBaseContext(), "SMS sent.", Toast.LENGTH_SHORT).show();

        return true;
     }
 }
}

1 个答案:

答案 0 :(得分:0)

我认为不需要在onFinish()方法中调用timer.start。