我是Android开发的新手,我无法理解这个错误,我正在制作一个简单的秒表应用程序,它有一个Chronometer和3个按钮(启动,停止,重置)我在主java文件中正确定义了所有这些用正确的标签。
它很奇怪,因为我之前测试了应用程序,我的应用程序工作正常,但随后我重新定位了GUI中的按钮,现在它说我没有正确地投射计时器。
06-06 19:11:07.488: E/AndroidRuntime(21511): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shitstopwatch/com.example.shitstopwatch.ShittyClockActivity}: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.Chronometer
这是主要的java文件:
public class ShittyClockActivity extends ActionBarActivity {
Button Start;
Button Stop;
Button Reset;
Chronometer mainChronometer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shitty_clock);
mainChronometer = (Chronometer) this.findViewById(R.id.mainChronometer);
Start = (Button) this.findViewById(R.id.startButton);
Stop = (Button) this.findViewById(R.id.stopButton);
Reset = (Button) this.findViewById(R.id.resetButton);
addButtonListeners();
}
private void addButtonListeners()
{
Start.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
int stoppedMilliseconds = 0;
mainChronometer.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds);
mainChronometer.start();
}
});
Stop.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
mainChronometer.stop();
}
});
Reset.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
mainChronometer.setBase(SystemClock.elapsedRealtime());
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.shitty_clock, 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);
}public class ShittyClockActivity extends ActionBarActivity {
Button Start;
Button Stop;
Button Reset;
Chronometer mainChronometer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shitty_clock);
mainChronometer = (Chronometer) this.findViewById(R.id.mainChronometer);
Start = (Button) this.findViewById(R.id.startButton);
Stop = (Button) this.findViewById(R.id.stopButton);
Reset = (Button) this.findViewById(R.id.resetButton);
addButtonListeners();
}
private void addButtonListeners()
{
Start.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
int stoppedMilliseconds = 0;
mainChronometer.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds);
mainChronometer.start();
}
});
Stop.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
mainChronometer.stop();
}
});
Reset.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
mainChronometer.setBase(SystemClock.elapsedRealtime());
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.shitty_clock, 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);
}
以下是带有ID标记的活动XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
tools:context="com.example.shitstopwatch.ShittyClockActivity$PlaceholderFragment" >
<Chronometer
android:id="@+id/mainChronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:text="@string/chronometer"
android:textSize="@dimen/BigText" />
<Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/mainChronometer"
android:layout_below="@+id/mainChronometer"
android:layout_marginTop="63dp"
android:text="@string/startButton" />
<Button
android:id="@+id/resetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/startButton"
android:layout_below="@+id/startButton"
android:layout_marginTop="16dp"
android:text="@string/resetButton" />
<Button
android:id="@+id/stopButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/resetButton"
android:layout_below="@+id/resetButton"
android:layout_marginTop="20dp"
android:text="@string/stopButton" />
</RelativeLayout>
答案 0 :(得分:0)
清理项目(例如,项目&gt;在Eclipse中清理)。
有时,R
常量与APK文件中的资源条目不同步,导致此错误。