그래서 나는 outComeButton와 onFinish 호출하기 위해 노력하고있어 :전화는 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" />
또한 오류 내가하려고 할 때 로그 캣에서 얻을 버튼을 누르 :
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'에 대한 당신의 정의는 당신이 그 방법을 정의한 곳에'Activity'에 의해 사용 된 것과 같은 XML 레이아웃 파일에 있는가? 옳은? – Squonk