我试图制作一个计时器应用程序,但遇到了一个障碍。当我尝试单击按钮时,它不执行其任务。 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
此后,当我尝试在模拟器中运行应用程序并单击“执行”按钮时,它在日志猫中不显示任何内容。
最佳答案
您正在TextView
而不是Button
上设置事件处理程序。
<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 - onClick函数在android studio中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38162709/