将onFinish称为按钮外部计时器

时间:2014-01-19 00:49:25

标签: java android

所以我试图用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

2 个答案:

答案 0 :(得分:0)

你在XML布局文件中指向的onClick必须是使用按钮给视图充气的Activity的一部分。

将方法提取到类中并从那里完成计时器,一切都应该正常工作。

一般来说,在XML中声明onClick是不好的做法,应该避免使用。

答案 1 :(得分:0)

“从不使用本地”警告来自匿名outComeButton()内部类中的第二个OnClickListener方法。

在XML中定义onClick侦听器要求在活动中找到它。如果您删除或注释掉了第一个活动级outComeButton()方法,那么这将解释堆栈跟踪中的异常。

删除内部outComeButton(),并在活动级别方法中执行您想要完成的操作。