1

so my stopwatch class worked fine until I made some changes in XML code of it. Basiclly what I did is change some sizes and layout_margins..nothing special

heres the xml code, and 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>

heres the logcat

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

here's java code of this activity

 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());




            }
        }


    }
5
  • 1
    post your activity code if you can!
    – gsb
    Jul 12, 2012 at 20:57
  • Can you show an example of the relevant XML that you changed?
    – Alex W
    Jul 12, 2012 at 20:57
  • There is a problem on line no 30 of your StopwatchMenu.java. Post that line.
    – gsb
    Jul 12, 2012 at 20:58
  • Basically i was playing with margins, and i put the chronometer inside his own linear layout... Jul 12, 2012 at 21:03
  • what is line no 23 and line no 30 of your activity code?
    – gsb
    Jul 12, 2012 at 21:08

2 Answers 2

2

Have you tried cleaning your project?

2
  • ok, thank you very much, simple clean up was needed and it works as before... can you just tell me, why this was needed? Jul 12, 2012 at 21:54
  • You're welcome :-). From my experience it just seems like the ADT plugin doesn't always gracefully handle changes to xml resources. I don't know the specific reason why
    – TJ Thind
    Jul 12, 2012 at 22:37
0

Your problem is probably stopwatch = (Chronometer) findViewById(R.id.chronometer1);

You're attempting to cast a View as a Chronometer. Your LogCat says that a ClassCastException was thrown, which means that casting Views as Chronometer probably isn't supported.

Alternately, something could be going wrong with the stopwatch.setBase method. You might be casting a long or double as an int or vice-versa. Look at your method parameters and returns to make sure you're matching up the proper parameter types.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.