【问题标题】:Why my stopwatch isn't working in Android Studio?为什么我的秒表在 Android Studio 中不起作用?
【发布时间】:2014-10-24 06:37:27
【问题描述】:

我正在尝试使用this 教程在 Android Studio 中为我的应用程序创建秒表,当应用程序本身运行顺利时,当我按下“开始”按钮时没有任何反应。

下面是我的代码:

activity_third.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/stopwatch"
    android:textSize="20sp"/>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">

    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/start"/>

    <Button
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/stop"/>

</LinearLayout>

<Chronometer
    android:id="@+id/chronometer"
    android:format="%s"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="40sp"/>

ThirdActivity.java:

public class ThirdActivity extends Activity implements OnClickListener {
    private Chronometer chronometer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);

        chronometer = (Chronometer) findViewById(R.id.chronometer);
        findViewById(R.id.start).setOnClickListener(this);
        findViewById(R.id.stop).setOnClickListener(this);
    }

    @Override
    public void onClick(View v){
        switch(v.getId()){
            case R.id.start:
                chronometer.setBase(SystemClock.elapsedRealtime());
                chronometer.start();
                break;
            case R.id.stop:
                chronometer.stop();
                break;
        }
    }
}

谁能告诉我为什么这些按钮不起作用以及如何修复它们?谢谢!

【问题讨论】:

  • 有错误提示吗?
  • 否——应用在模拟器上运行正常;只是按钮似乎不起作用。
  • 我尝试用你的代码运行,它运行正常。您可以将 LogCat Log.d("DEBUG", "start") 放入您的开关盒中,以检查按钮 onClickListener 是否有效?
  • 我按照您的要求做了,但“开始”按钮仍然不起作用...不过,感谢您的尝试! :)
  • 检查传入 onClick switch 语句的 ID 是否与起始 ID 匹配

标签: java android stopwatch


【解决方案1】:

我使用了您在上面发布的代码,它可以工作,唯一的问题是完整的布局不是复制和粘贴的。

检查活动是否实现了 View.onClickListener 而不是另一个。

这是我使用的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Stopwatch"
    android:textSize="20sp"/>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">

    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start"/>

    <Button
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stop"/>

</LinearLayout>
    <Chronometer
        android:id="@+id/chronometer"
        android:format="%s"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40sp"/>

</LinearLayout>


public class TestActivity extends Activity implements View.OnClickListener {
private Chronometer chronometer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    chronometer = (Chronometer) findViewById(R.id.chronometer);
    findViewById(R.id.start).setOnClickListener(this);
    findViewById(R.id.stop).setOnClickListener(this);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.test, 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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View v){
    switch(v.getId()){
        case R.id.start:
            chronometer.setBase(SystemClock.elapsedRealtime());
            chronometer.start();
            break;
        case R.id.stop:
            chronometer.stop();
            break;
    }
}

}

此代码有效,它启动了计时器显示到屏幕上。

【讨论】:

  • 我在一个新项目中尝试了你的代码,它可以工作,但是由于某种原因它在我自己的项目中不起作用... ThirdActivity 是 MainActivity 的子活动;你认为这会影响什么吗?
  • 应该不会真正影响它,看起来按钮没有被绑定。 Activity 是否确实实现了 View.OnClickListener ?
  • 我怎么知道 View.OnClickListener 是否已经实现了?代码和你说的完全一样,如果这就是你的意思......在我的 logcat 上,它说,“跳过 136 帧!应用程序可能在其主线程上做太多工作。”你认为这可能是我的错误的根源吗?如果是这样,你知道如何解决这个问题吗?谢谢!
  • 我认为您正在实施正确的侦听器。很难说为什么它没有发生可能有很多原因。您是否使用断点调试过您的代码以查看启动秒表代码是否曾经执行过?
猜你喜欢
  • 2020-01-16
  • 2016-04-19
  • 1970-01-01
  • 2020-01-11
  • 2020-05-10
  • 1970-01-01
  • 2013-08-16
  • 2021-11-24
  • 2015-08-13
相关资源
最近更新 更多