所以我想打電話給onFinish與outComeButton:呼叫onFinish的按鈕外定時器
import java.util.concurrent.TimeUnit;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Vibrator;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class OutCome0 extends Activity {
private CountDownTimer countDownTimer;
private boolean timerHasStarted = false;
public TextView text;
private final long interval = 1 * 1000;
EditText editTime1;
Button startButton;
/*Skip go straight to an outcome
* To Do Instead of having two sets of the same shit, call onFinish here.*/
public void outComeButton(View v){
;
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_out_come0);
editTime1 = (EditText)findViewById(R.id.editTime1);
startButton = (Button)findViewById(R.id.startButton);
text = (TextView) this.findViewById(R.id.timer);
startButton.setOnClickListener(new OnClickListener() {
/*Start the timer */
public void onClick(View v) {
// get the name from edittext and storing into string variable
long timeVal = Long.parseLong(editTime1.getText().toString());
/* the keyboard wasen't auto hiding. below three lines force it closed.*/
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(startButton.getWindowToken(), 0);
if (!timerHasStarted) {
countDownTimer = new MyCountDownTimer(timeVal * 1000 * 60, interval);
text.setText(text.getText() + String.valueOf(timeVal/1000));
countDownTimer.start();
timerHasStarted = true;
startButton.setText("STOP");
} else {
countDownTimer.cancel();
timerHasStarted = false;
startButton.setText("RESTART");
}
}
class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long timeVal, long interval) {
super(timeVal, interval);
}
public void outComeButton(View v){
this.onFinish();
};
/* Converts tick time to 00:00 need to fix so that 1 min 30 seconds is 01:30 instead of 01:3
* Currently hidden, maybe though do somthing with onTick later.*/
@Override
public void onTick(long millisUntilFinished) {
/* text.setText(""+String.format("%d:%d",
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))
));*/
}
@Override
/* Finish event Need to make it so that videos can be played if solo mode.*/
public void onFinish() {
int num;
String outCome = "";
for (int ii = 0; ii < 10; ii++) {
num = (int) (Math.random() * 100) % 4;
switch (num) {
case 0:
case 1:
outCome = "Outcome1";
break;
case 2:
outCome = "outcome2";
break;
case 3:
outCome = "outcome3";
break;
}
text.setText(outCome);
}
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 5 seconds
v.vibrate(5000);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.out_come0, menu);
return true;
}
}
它抱怨說outComeButton從未在本地使用的方法。我的XML有沒有問題,或者我把按鈕放在了錯誤的地方?
<Button
android:id="@+id/outComeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="outComeButton"
android:text="@string/outComeButton" />
而且錯誤,我從logcat中獲得當我嘗試按下按鈕:
01-19 01:02:50.507: E/AndroidRuntime(379): FATAL EXCEPTION: main
01-19 01:02:50.507: E/AndroidRuntime(379): java.lang.IllegalStateException: Could not find a method outComeButton(View) in the activity class com.example.timer.OutCome0 for onClick handler on view class android.widget.Button with id 'outComeButton'
01-19 01:02:50.507: E/AndroidRuntime(379): at android.view.View$1.onClick(View.java:2131)
01-19 01:02:50.507: E/AndroidRuntime(379): at android.view.View.performClick(View.java:2485)
01-19 01:02:50.507: E/AndroidRuntime(379): at android.view.View$PerformClick.run(View.java:9080)
01-19 01:02:50.507: E/AndroidRuntime(379): at android.os.Handler.handleCallback(Handler.java:587)
01-19 01:02:50.507: E/AndroidRuntime(379): at android.os.Handler.dispatchMessage(Handler.java:92)
01-19 01:02:50.507: E/AndroidRuntime(379): at android.os.Looper.loop(Looper.java:123)
01-19 01:02:50.507: E/AndroidRuntime(379): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-19 01:02:50.507: E/AndroidRuntime(379): at java.lang.reflect.Method.invokeNative(Native Method)
01-19 01:02:50.507: E/AndroidRuntime(379): at java.lang.reflect.Method.invoke(Method.java:507)
01-19 01:02:50.507: E/AndroidRuntime(379): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-19 01:02:50.507: E/AndroidRuntime(379): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-19 01:02:50.507: E/AndroidRuntime(379): at dalvik.system.NativeStart.main(Native Method)
01-19 01:02:50.507: E/AndroidRuntime(379): Caused by: java.lang.NoSuchMethodException: outComeButton
01-19 01:02:50.507: E/AndroidRuntime(379): at java.lang.ClassCache.findMethodByName(ClassCache.java:247)
01-19 01:02:50.507: E/AndroidRuntime(379): at java.lang.Class.getMethod(Class.java:962)
01-19 01:02:50.507: E/AndroidRuntime(379): at android.view.View$1.onClick(View.java:2124)
01-19 01:02:50.507: E/AndroidRuntime(379): ... 11 more
如果您在佈局XML文件中使用'android:onClick'指定偵聽器,則該方法(在代碼中)必須包含一個包含'View'作爲參數的簽名,例如'public void outComeButton(View v)' 。 – Squonk
我編輯它,我得到同樣的問題... – cflinspach
你的'outComeButton'的定義是在同一個XML佈局文件中,由你定義方法的'Activity'使用?正確? – Squonk