2010-12-11 89 views
1

我一直在嘗試調用另一個類的方法。如何從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; 
    } 
} 
} 
+0

可以發佈什麼樣的錯誤打印在日誌貓 – Kakey 2010-12-11 13:53:51

+0

12-11 22:01:56.845:錯誤/系統(65):失敗開始核心服務 12-11 22:01:56.845:錯誤/系統(65 ):java.lang.SecurityException 12-11 22:01:56.845:ERROR/System(65):at android.os.BinderProxy.transact(Native Method) 12-11 22:01:56.845:ERROR /系統(65):在android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146) 12-11 22:01:56.845:錯誤/系統(65):在android.os.ServiceManager.addService(ServiceManager.java :72) 12-11 22:01:56.845:錯誤/系統(65):at com.android.server.ServerThread.run(SystemServer.jav a:184) – user538889 2010-12-11 14:32:48

回答

0

我覺得沒有必要再調用timer.start在onFinish()方法。

+0

onFinish()中的timer.start將在計數器完成後重新啓動計時器 – user538889 2010-12-11 14:00:30

+0

顯示的錯誤是什麼?你可以發佈嗎? – Kakey 2010-12-11 14:04:45

+0

12-11 22:01:18.875:ERROR/Zygote(32):setreuid()失敗。 errno:2 12-11 22:01:38.045:ERROR/Zygote(32):setreuid()失敗。 errno:17 – user538889 2010-12-11 14:12:48

相關問題