祝大家开心。
我一直在尝试使用Chronometer
函数制作一个秒表应用程序,这是我的第一个应用程序。问题是,当我单击“开始”按钮(应用程序在平板电脑上运行时)时,崩溃了。情况与其他情况相似。
我不确定如何解决此问题,非常感谢您的帮助。
activity_main.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"
tools:context=".MainActivity">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
<ImageView
android:id="@+id/body"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="@color/colorPrimaryDark" />
<LinearLayout
android:id="@+id/navcon"
android:layout_width="match_parent"
android:layout_height="92dp"
android:orientation="vertical">
<ImageView
android:id="@+id/navbar"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@color/colorPrimary" />
</LinearLayout>
<LinearLayout
android:id="@+id/navtextcon"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="vertical">
<TextView
android:id="@+id/navtext"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="rocket.studios"
android:textColor="@android:color/white"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
</FrameLayout>
<LinearLayout
android:id="@+id/chronocon"
android:layout_width="match_parent"
android:layout_height="95dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="78dp"
android:orientation="vertical">
<Chronometer
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:textColor="@android:color/white"
android:textSize="60sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="@+id/startbcon"
android:layout_width="match_parent"
android:layout_height="118dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="155dp"
android:orientation="vertical"
android:padding="20dp">
<Button
android:id="@+id/startb"
android:layout_width="match_parent"
android:layout_height="68dp"
android:background="@color/colorPrimary"
android:text="@string/start"
android:textColor="@android:color/white"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="@+id/stopbcon"
android:layout_width="match_parent"
android:layout_height="118dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:orientation="vertical"
android:padding="20dp">
<Button
android:id="@+id/stopb"
android:layout_width="match_parent"
android:layout_height="68dp"
android:background="@color/colorPrimary"
android:text="@string/stop"
android:textColor="@android:color/white"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="@+id/resetbcon"
android:layout_width="match_parent"
android:layout_height="118dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="158dp"
android:orientation="vertical"
android:padding="20dp">
<Button
android:id="@+id/resetb"
android:layout_width="match_parent"
android:layout_height="68dp"
android:background="@color/colorPrimary"
android:text="@string/reset"
android:textColor="@android:color/white"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
MainActivity.java:
package testing.ezekieralt.com.stopwatcher;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ButtonBarLayout;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.os.SystemClock;
//chronometer is...guess it
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Chronometer chronometer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.startb).setOnClickListener(this);
findViewById(R.id.resetb).setOnClickListener(this);
findViewById(R.id.stopb).setOnClickListener(this);
chronometer = findViewById(R.id.chronometer);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.stopb:
chronometer.stop();
break;
case R.id.startb:
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();
break;
case R.id.resetb:
chronometer.setBase(SystemClock.elapsedRealtime());
break;
}
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="testing.ezekieralt.com.stopwatcher">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
最佳答案
您没有将id
添加到Chronometer
中。将ID添加到Chronometer
<Chronometer
android:id="@+id/chronometer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:textColor="@android:color/white"
android:textSize="60sp"
android:textStyle="bold" />