2016-07-02 7 views
1

私はタイマーアプリを作ろうとしていましたが、ハードルを見つけました。ボタンをクリックしようとすると、そのタスクを実行しません。 onClick関数はどこかで間違っているようです。このPOI後アンドロイドスタジオでonClick機能が動作しません

MainActivity.java

package toast.akappstudio.com.eggtimer; 

    import android.os.Bundle; 
    import android.support.v7.app.AppCompatActivity; 
    import android.util.Log; 
    import android.view.Menu; 
    import android.view.MenuItem; 
    import android.view.View; 
    import android.widget.SeekBar; 
    import android.widget.TextView; 

    public class MainActivity extends AppCompatActivity { 
     SeekBar timerSeekBar; 
     TextView timerTextView; 
     public void updateTimer(int secondsLeft){ 

      int minutes = (int) secondsLeft/60; 
      int seconds = secondsLeft - minutes * 60; 




      timerTextView.setText(Integer.toString(minutes) + ":" + Integer.toString(seconds)); 

     } 


     public void controlTimer(View view){ 

      Log.i("Work","Yes"); 

      /* new CountDownTimer(timerSeekBar.getProgress() * 1000, 1000){ 

       @Override 
       public void onTick(long millisUntilFinished) { 

        updateTimer((int) millisUntilFinished/1000); 

       } 

       @Override 
       public void onFinish() { 
        Log.i("Finished", "Done"); 
       } 
      }.start(); 
    */ 

     } 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      timerSeekBar = (SeekBar)findViewById(R.id.timerSeekBar); 
      timerTextView = (TextView) findViewById(R.id.timerTextView); 



      timerSeekBar.setMax(600); 
      timerSeekBar.setProgress(30); 


      timerSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 
       @Override 
       public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 

        updateTimer(progress); 
       } 

       @Override 
       public void onStartTrackingTouch(SeekBar seekBar) { 

       } 

       @Override 
       public void onStopTrackingTouch(SeekBar seekBar) { 

       } 
      }); 


     } 

     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      // Inflate the menu; this adds items to the action bar if it is present. 
      getMenuInflater().inflate(R.menu.menu_main, menu); 
      return true; 
     } 

     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 
      // Handle action bar item clicks here. The action bar will 
      // automatically handle clicks on the Home/Up button, so long 
      // as you specify a parent activity in AndroidManifest.xml. 
      int id = item.getItemId(); 

      //noinspection SimplifiableIfStatement 
      if (id == R.id.action_settings) { 
       return true; 
      } 

      return super.onOptionsItemSelected(item); 
     } 
    } 

content.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="toast.akappstudio.com.eggtimer.MainActivity" 
    tools:showIn="@layout/activity_main"> 

    <SeekBar 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/timerSeekBar" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:indeterminate="false" /> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/imageView" 
     android:src="@drawable/egg" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:layout_marginBottom="21dp" 
     android:layout_below="@+id/timerSeekBar" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="0:30" 
     android:id="@+id/timerTextView" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" 
     android:singleLine="true" 
     android:textSize="70sp" 
     android:onClick="controlTimer" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="GO!" 
     android:id="@+id/controllerButton" 
     android:layout_marginTop="55dp" 
     android:layout_below="@+id/timerTextView" 
     android:layout_centerHorizontal="true" /> 
</RelativeLayout> 

LOGCAT

07-02 13:11:32.074 1999-1999/? D/dalvikvm: Late-enabling CheckJNI 
07-02 13:11:32.170 1999-1999/toast.akappstudio.com.eggtimer W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;) 
07-02 13:11:32.170 1999-1999/toast.akappstudio.com.eggtimer I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested 
07-02 13:11:32.170 1999-1999/toast.akappstudio.com.eggtimer W/dalvikvm: VFY: unable to resolve interface method 17962: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z 
07-02 13:11:32.170 1999-1999/toast.akappstudio.com.eggtimer D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 
07-02 13:11:32.170 1999-1999/toast.akappstudio.com.eggtimer I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode 
07-02 13:11:32.170 1999-1999/toast.akappstudio.com.eggtimer W/dalvikvm: VFY: unable to resolve interface method 17966: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode; 
07-02 13:11:32.170 1999-1999/toast.akappstudio.com.eggtimer D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 
07-02 13:11:32.250 1999-1999/toast.akappstudio.com.eggtimer I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations 
07-02 13:11:32.250 1999-1999/toast.akappstudio.com.eggtimer W/dalvikvm: VFY: unable to resolve virtual method 421: Landroid/content/res/TypedArray;.getChangingConfigurations()I 
07-02 13:11:32.250 1999-1999/toast.akappstudio.com.eggtimer D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 
07-02 13:11:32.250 1999-1999/toast.akappstudio.com.eggtimer I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType 
07-02 13:11:32.250 1999-1999/toast.akappstudio.com.eggtimer W/dalvikvm: VFY: unable to resolve virtual method 443: Landroid/content/res/TypedArray;.getType (I)I 
07-02 13:11:32.250 1999-1999/toast.akappstudio.com.eggtimer D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 
07-02 13:11:32.278 1999-1999/toast.akappstudio.com.eggtimer D/dalvikvm: GC_FOR_ALLOC freed 174K, 7% free 3355K/3608K, paused 3ms, total 3ms 
07-02 13:11:32.278 1999-1999/toast.akappstudio.com.eggtimer I/dalvikvm-heap: Grow heap (frag case) to 5.590MB for 2349444-byte allocation 
07-02 13:11:32.286 1999-2008/toast.akappstudio.com.eggtimer D/dalvikvm: GC_FOR_ALLOC freed 4K, 5% free 5645K/5904K, paused 6ms, total 6ms 
07-02 13:11:32.426 1999-1999/toast.akappstudio.com.eggtimer D/libEGL: loaded /system/lib/egl/libEGL_genymotion.so 
07-02 13:11:32.454 1999-1999/toast.akappstudio.com.eggtimer D/libEGL: loaded /system/lib/egl/libGLESv1_CM_genymotion.so 
07-02 13:11:32.458 1999-1999/toast.akappstudio.com.eggtimer D/libEGL: loaded /system/lib/egl/libGLESv2_genymotion.so 
07-02 13:11:32.502 1999-1999/toast.akappstudio.com.eggtimer W/EGL_genymotion: eglSurfaceAttrib not implemented 
07-02 13:11:32.506 1999-1999/toast.akappstudio.com.eggtimer E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from GradienCache 
07-02 13:11:32.506 1999-1999/toast.akappstudio.com.eggtimer E/OpenGLRenderer: MAX_TEXTURE_SIZE: 16384 
07-02 13:11:32.510 1999-1999/toast.akappstudio.com.eggtimer E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from Caches::initConstraints() 
07-02 13:11:32.514 1999-1999/toast.akappstudio.com.eggtimer E/OpenGLRenderer: MAX_TEXTURE_SIZE: 16384 
07-02 13:11:32.514 1999-1999/toast.akappstudio.com.eggtimer D/OpenGLRenderer: Enabling debug mode 0 

私がエミュレータでアプリケーションを実行しようとすると、goボタ​​ンをクリックしてもログcatに何も表示されません。

答えて

2

にないイベントハンドラをTextViewに設定しています。

<TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="0:30" 
     android:id="@+id/timerTextView" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" 
     android:singleLine="true" 
     android:textSize="70sp" 
     android:onClick="controlTimer" /> 

Buttonに設定してください。

+0

はそれは私があまりにも愚かだった....ありがとうございました!!! @ giannisf:しかし、あなたはまだスニペット以下でごcontrolTimer(View)方法を置き換えることによって、それを動作させることができます –

0

あなたのコードのボタンにクリックリスナーを設定していないか、簡潔にするためにコードにそのコードを含めていませんか?それが当てはまらない場合は、あなたは、あなたの活動の二つの方法により、ボタンにリスナーをクリックして設定することができます。

方法1

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    ...... 

    Button controllerButton = (Button) findViewById(R.id.controllerButton); 
    controllerButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //Code you want to execute on click of a button 
     } 
    }); 
} 

方法2

content.xml

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="GO!" 
    android:id="@+id/controllerButton" 
    android:layout_marginTop="55dp" 
    android:layout_below="@+id/timerTextView" 
    android:layout_centerHorizontal="true" 
    android:onClick="onClick" /> 

MainActivity.java

public void onClick(View view) { 
    switch(view.getId()) { 
     case R.id.controllerButton: 
      //Code you want to execute on click of a button 
      break; 
    } 
} 
0

エラーの原因は、あなたのボタンのコードです。 android:onClick=""なしでxmlレイアウトでボタンを作成している限り、Javaファイル内でonClick(View)を使用することはできません。

Button btn = (Button)this.findViewById(R.id.controlTimer); 

btn.setOnClickListener(new View.OnClickListener(){ 

new CountDownTimer(timerSeekBar.getProgress() * 1000, 1000){ 

      @Override 
      public void onTick(long millisUntilFinished) { 

       updateTimer((int) millisUntilFinished/1000); 

      } 

      @Override 
      public void onFinish() { 
       Log.i("Finished", "Done"); 
      } 
     }.start(); 
}); 
関連する問題