Im trying to make my first application to calcaulate physical activities for me. At first, ive tried to make simple chronometer with start and stop button.
XML:
<Core:CallMethodAction MethodName="GotoDetailsPage" TargetObject="{Binding ElementName=ViewModel}" />
and of course activity.java
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0">
<ImageView
android:layout_width="107dp"
android:layout_height="93dp"
android:id="@+id/Fit_logo"
android:src="@drawable/fitapp_gray"
android:layout_marginLeft="30dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Current time"
android:id="@+id/CurrentTime"
android:layout_gravity="center_horizontal" />
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/time"
android:layout_gravity="center_horizontal"
android:textSize="25sp"
android:textColor="#ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Degree"
android:id="@+id/degree"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="City"
android:id="@+id/city"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="vertical"
android:layout_weight="1"
android:layout_marginBottom="100dp"
android:layout_gravity="center">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Duration"
android:id="@+id/duration"
android:gravity="center_horizontal"
android:layout_marginTop="10dp" />
<Chronometer
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chronometer"
android:layout_gravity="center_horizontal"
android:textSize="30sp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Calories"
android:id="@+id/calories"
android:layout_weight="1"
android:gravity="center"
android:layout_marginTop="50dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Steps"
android:id="@+id/steps"
android:layout_weight="1"
android:gravity="center"
android:layout_marginTop="50dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Distance"
android:id="@+id/distance"
android:layout_weight="1"
android:gravity="center"
android:layout_marginTop="50dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center|center_horizontal">
<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" />
<Space
android:layout_width="100dp"
android:layout_height="wrap_content" />
<ImageButton
android:layout_width="100dip"
android:layout_height="100dip"
android:id="@+id/btnStop"
android:background="@drawable/stopbutton"
android:clickable="true"
android:layout_marginBottom="70dp"
android:layout_gravity="bottom|center_horizontal" />
</LinearLayout>
</LinearLayout>
but everytime I try to run the app it force closes itself just before start.
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();
}
});
Fatal error is for Start button. Could anybody kick me in right direction what else to do? Thanks
答案 0 :(得分:0)
好的,我已经制作了包含所有参数的全新项目。制作新的工具方法,现在可以使用了。感谢您的帮助,我感谢所有答案;)
答案 1 :(得分:-1)
我认为问题在于您是否正在尝试从支持库转换为小部件。更改导入,使两者兼容。
答案 2 :(得分:-1)
看来你正在尝试以错误的方式投射AppCompatButton Button ImageButton
投射错误意味着你尝试识别不同类型的属性
Ex:在您的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" />
在java中
// here is casting
Button mStartButton = (Button) findViewById(R.id.btnStart);
问题是在xml中你将它定义为ImageButton
但在java中却将其定义为Button
因此它崩溃并告诉你不能转换为不同的类型