有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法启动活动。计时器启动按钮

我正试图做我的第一个申请,为我计算体育活动。起初,我试着做一个带有启动和停止按钮的简单计时器

XML:

<LinearLayout
    安卓:orientation="horizontal"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:layout_weight="0">

    <ImageView
        安卓:layout_width="107dp"
        安卓:layout_height="93dp"
        安卓:id="@+id/Fit_logo"
        安卓:src="@drawable/fitapp_gray"
        安卓:layout_marginLeft="30dp" />

    <LinearLayout
        安卓:orientation="vertical"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_marginLeft="30dp">

        <TextView
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:textAppearance="?安卓:attr/textAppearanceSmall"
            安卓:text="Current time"
            安卓:id="@+id/CurrentTime"
            安卓:layout_gravity="center_horizontal" />

        <TextClock
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:id="@+id/time"
            安卓:layout_gravity="center_horizontal"
            安卓:textSize="25sp"
            安卓:textColor="#ffffff" />

        <TextView
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:textAppearance="?安卓:attr/textAppearanceMedium"
            安卓:text="Degree"
            安卓:id="@+id/degree"
            安卓:layout_gravity="center_horizontal" />

        <TextView
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:textAppearance="?安卓:attr/textAppearanceMedium"
            安卓:text="City"
            安卓:id="@+id/city"
            安卓:layout_gravity="center_horizontal" />
    </LinearLayout>

</LinearLayout>

<LinearLayout
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:background="@安卓:color/white"
    安卓:orientation="vertical"
    安卓:layout_weight="1"
    安卓:layout_marginBottom="100dp"
    安卓:layout_gravity="center">

    <TextView
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:textAppearance="?安卓:attr/textAppearanceSmall"
        安卓:text="Duration"
        安卓:id="@+id/duration"
        安卓:gravity="center_horizontal"
        安卓:layout_marginTop="10dp" />

    <Chronometer
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:id="@+id/chronometer"
        安卓:layout_gravity="center_horizontal"
        安卓:textSize="30sp" />

    <LinearLayout
        安卓:orientation="horizontal"
        安卓:layout_width="match_parent"
        安卓:layout_height="match_parent"
        安卓:layout_gravity="center">

        <TextView
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:textAppearance="?安卓:attr/textAppearanceSmall"
            安卓:text="Calories"
            安卓:id="@+id/calories"
            安卓:layout_weight="1"
            安卓:gravity="center"
            安卓:layout_marginTop="50dp" />

        <TextView
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:textAppearance="?安卓:attr/textAppearanceSmall"
            安卓:text="Steps"
            安卓:id="@+id/steps"
            安卓:layout_weight="1"
            安卓:gravity="center"
            安卓:layout_marginTop="50dp" />

        <TextView
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:textAppearance="?安卓:attr/textAppearanceSmall"
            安卓:text="Distance"
            安卓:id="@+id/distance"
            安卓:layout_weight="1"
            安卓:gravity="center"
            安卓:layout_marginTop="50dp" />
    </LinearLayout>

</LinearLayout>

<LinearLayout
    安卓:orientation="horizontal"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:gravity="center_vertical|center|center_horizontal">

    <ImageButton
        安卓:layout_width="100dip"
        安卓:layout_height="100dip"
        安卓:id="@+id/btnStart"
        安卓:background="@drawable/startbutton"
        安卓:clickable="true"
        安卓:layout_marginBottom="70dp"
        安卓:layout_gravity="bottom|center_horizontal" />

    <Space
        安卓:layout_width="100dp"
        安卓:layout_height="wrap_content" />

    <ImageButton
        安卓:layout_width="100dip"
        安卓:layout_height="100dip"
        安卓:id="@+id/btnStop"
        安卓:background="@drawable/stopbutton"
        安卓:clickable="true"
        安卓:layout_marginBottom="70dp"
        安卓:layout_gravity="bottom|center_horizontal" />
</LinearLayout>

</LinearLayout>

当然还有活动。爪哇

final Chronometer chronometer =  (Chronometer)  findViewById(R.id.chronometer);
final ImageButton start = (ImageButton) findViewById(R.id.btnStart);
final ImageButton stop = (ImageButton) findViewById(R.id.btnStop);


start.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        chronometer.start();
        Toast.makeText(Activity_cal.this, "Chronometer Started", Toast.LENGTH_SHORT).show();
    }
});
 stop.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    chronometer.stop();
    }
});

但每次我尝试运行应用程序时,它都会在启动前自动关闭

Caused by: java.lang.ClassCastException:安卓.support.v7.widget.AppCompatButton cannot be cast to 安卓.widget.ImageButton 

at com.example.dewewors.fitness.Activity_cal.onCreate(Activity_cal.java:31)

“开始”按钮出现致命错误。 有人能把我踢向正确的方向吗?还能做什么?谢谢


共 (3) 个答案

  1. # 1 楼答案

    似乎您试图以错误的方式投射AppCompatButton ImageButton

    强制转换时出错意味着您尝试在不同类型中标识属性

    例:在xml中

     <ImageButton
            android:layout_width="100dip"
            android:layout_height="100dip"
            android:id="@+id/btnStart"
            android:background="@drawable/startbutton"
            android:clickable="true"
            android:layout_marginBottom="70dp"
            android:layout_gravity="bottom|center_horizontal" />
    

    在爪哇

                  //  here is casting
    Button mStartButton = (Button) findViewById(R.id.btnStart);
    

    问题是,在xml中,您将其定义为ImageButton,但在java中,您将其定义为Button,因此它会崩溃,并告诉您不能转换为其他类型

  2. # 2 楼答案

    我认为问题在于您试图从支持库中转换到一个小部件,但事实并非如此。更改导入,使两者兼容

  3. # 3 楼答案

    好的,我用所有这些参数做了一个新项目。提出了新的实现方法,现在可以使用了。谢谢你的帮助,我很感激你的回答;)