所以我的秒表类运行良好,直到我对它的 XML 代码进行了一些更改。基本上我所做的是改变一些尺寸和 layout_margins..没什么特别的
继承人的xml代码和logcat
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Chronometer
android:id="@+id/chronometer1"
android:layout_width="57dp"
android:layout_height="49dp"
android:text="Chronometer"
android:layout_marginTop="150dp"
android:layout_gravity="center"
android:textSize="50dp"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/bt_sm_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:layout_marginLeft="25dp"
android:textSize="20dp"
android:layout_marginTop="50dp"
/>
<Button
android:id="@+id/bt_sm_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_marginTop="50dp"
android:text="Pause"
android:layout_marginLeft="25dp"
/>
<Button
android:id="@+id/bt_sm_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:textSize="20dp"
android:text="Stop"
android:layout_marginLeft="25dp"
/>
</LinearLayout>
</LinearLayout>
这是日志猫
07-12 20:35:46.253: I/Process(360): Sending signal. PID: 360 SIG: 9
07-12 20:37:25.973: D/AndroidRuntime(395): Shutting down VM
07-12 20:37:25.973: W/dalvikvm(395): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-12 20:37:25.993: E/AndroidRuntime(395): FATAL EXCEPTION: main
07-12 20:37:25.993: E/AndroidRuntime(395): java.lang.RuntimeException: Unable to start activity ComponentInfo{mioc.diver/mioc.diver.StopwatchMenu}: java.lang.ClassCastException: android.widget.Chronometer
07-12 20:37:25.993: E/AndroidRuntime(395): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-12 20:37:25.993: E/AndroidRuntime(395): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-12 20:37:25.993: E/AndroidRuntime(395): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-12 20:37:25.993: E/AndroidRuntime(395): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-12 20:37:25.993: E/AndroidRuntime(395): at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 20:37:25.993: E/AndroidRuntime(395): at android.os.Looper.loop(Looper.java:123)
07-12 20:37:25.993: E/AndroidRuntime(395): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-12 20:37:25.993: E/AndroidRuntime(395): at java.lang.reflect.Method.invokeNative(Native Method)
07-12 20:37:25.993: E/AndroidRuntime(395): at java.lang.reflect.Method.invoke(Method.java:507)
07-12 20:37:25.993: E/AndroidRuntime(395): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-12 20:37:25.993: E/AndroidRuntime(395): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-12 20:37:25.993: E/AndroidRuntime(395): at dalvik.system.NativeStart.main(Native Method)
07-12 20:37:25.993: E/AndroidRuntime(395): Caused by: java.lang.ClassCastException: android.widget.Chronometer
07-12 20:37:25.993: E/AndroidRuntime(395): at mioc.diver.StopwatchMenu.Create(StopwatchMenu.java:30)
07-12 20:37:25.993: E/AndroidRuntime(395): at mioc.diver.StopwatchMenu.onCreate(StopwatchMenu.java:23)
07-12 20:37:25.993: E/AndroidRuntime(395): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-12 20:37:25.993: E/AndroidRuntime(395): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-12 20:37:25.993: E/AndroidRuntime(395): ... 11 more
这是这个活动的java代码
package mioc.diver;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
public class StopwatchMenu extends Activity implements OnClickListener{
int pause1 = 0;
long stopped = 0;
Button start;
Button stop;
Button pause;
Chronometer stopwatch;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stopwatch_menu);
Create();
}
private void Create() {
// TODO Auto-generated method stub
start = (Button) findViewById(R.id.bt_sm_start);
stop = (Button) findViewById(R.id.bt_sm_stop);
pause = (Button) findViewById(R.id.bt_sm_pause);
stopwatch = (Chronometer) findViewById(R.id.chronometer1);
start.setOnClickListener(this);
stop.setOnClickListener(this);
pause.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.bt_sm_start:
if(pause1 == 0)
stopwatch.setBase(SystemClock.elapsedRealtime());
//stopwatch.setBase(stopped);
stopwatch.start();
pause1 = 0;
break;
case R.id.bt_sm_pause:
stopwatch.stop();
stopped = 0;
stopped = stopwatch.getBase();
pause1 = 1;
break;
case R.id.bt_sm_stop:
stopwatch.stop();
stopwatch.setBase(SystemClock.elapsedRealtime());
}
}
}