2014-01-19 3 views
0

그래서 나는 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 
+0

레이아웃 XML 파일에'android : onClick'을 사용하여 리스너를 지정하면 (코드에서) 메서드는 매개 변수로 'View'를 포함하는 서명을 가져야합니다 (예 :'public void outComeButton (View v)'). . – Squonk

+0

나는 그것을 편집했고 나는 같은 문제를 얻는다. – cflinspach

+1

'outComeButton'에 대한 당신의 정의는 당신이 그 방법을 정의한 곳에'Activity'에 의해 사용 된 것과 같은 XML 레이아웃 파일에 있는가? 옳은? – Squonk

답변

0

온 클릭하면 팽창 년대 활동의 일부가 될 수 있어야 XML 레이아웃 파일을 가리키고를 단추가있는보기.

클래스에서 메소드를 추출하고 거기에서 타이머를 완료하면 모든 것이 올바르게 작동합니다.

일반적으로 XML에서 onClick을 선언하는 것은 바람직하지 않으므로 피해야합니다.

0

"never used locally"경고는 익명의 OnClickListener 내부 클래스의 두 번째 outComeButton() 메서드에서 발생합니다.

XML에서 onClick 수신기를 정의하려면 해당 수신기가 활동에 있어야합니다. 첫 번째 활동 수준 인 outComeButton() 메서드를 제거하거나 주석 처리 한 경우 스택 추적에서 예외가 설명됩니다.

outComeButton() 내부를 제거하고 활동 수준 방법에서 수행 할 작업을 수행하십시오.