0

FaceView.java

        public class FaceView extends View {
            private float radius;
            NormalFace normalFace;
            HappyFace happyFace;

            public FaceView(Context context, AttributeSet attrs) {
                super(context, attrs);

                // get radius value
                TypedArray a = context.getTheme().obtainStyledAttributes(
                        attrs,
                        R.styleable.FaceView,
                        0, 0
                );

                try {
                    radius = a.getDimension(R.styleable.FaceView_radius, 20.0f);
                } finally {
                    a.recycle();
                }

                // initiate HappyFace class
                normalFace = new NormalFace(radius);
                happyFace = new HappyFace(radius);
            }

            Handler setDelay;
            Runnable startDelay;

            @Override
            protected void onDraw(final Canvas canvas) {
                super.onDraw(canvas);
                normalFace.draw(canvas);
                //delay timer
                setDelay = new Handler();
                startDelay = new Runnable() {
                    @Override
                    public void run() {
                        happyFace.draw(canvas);
                    }
                };
                setDelay.postDelayed(startDelay,5000);
            }

FaceView.java is to display the different facial expressions in the canvas. I want to add a delay timer in FaceView.java so that the display of the normal face can be change to a happy face after the timer had finished counting down.

But the app was forced to shut down. Can anybody help me with this?

  • 1
    Do not ask same question twice. – ADM Dec 20 '17 at 08:00
  • 4
    Possible duplicate of [Add a delay timer in Android Studio](https://stackoverflow.com/questions/47899797/add-a-delay-timer-in-android-studio) – ADM Dec 20 '17 at 08:00
  • @ADM sorry I was asked to post it again. Anyway I have used Handler for the delay timer as shown in the code but the app was being forced shut down. Could you help? – Richelle Chia Dec 20 '17 at 08:07
  • There will be stacktrace posted in your IDE console. Can you post them here please? – A Nice Guy Dec 20 '17 at 08:12
  • @RichelleChia Take a look at this https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – Vygintas B Dec 20 '17 at 08:13
  • @Arnab A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x94 in tid 5330 (richelle.neeuro) [ 12-20 08:19:55.781 1194: 1194 W/ ] debuggerd: handling request: pid=5330 uid=10070 gid=10070 tid=5330 – Richelle Chia Dec 20 '17 at 08:20
  • @Arnab the program did run the normal face and delayed according to the timer but it was forced shut down before switching to the happy face – Richelle Chia Dec 20 '17 at 08:21
  • Remove `normalFace.draw(canvas);` from onDraw and put it below `happyFace = new HappyFace(radius);` and see if there is any difference. – Sunil Sunny Dec 20 '17 at 09:09
  • @sunilsunny but canvas is from the onDraw method – Richelle Chia Dec 20 '17 at 09:16
  • @sunilsunny i tried but... E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.richelle.neeuro, PID: 3433 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.richelle.neeuro/com.example.richelle.neeuro.MainActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class com.example.richelle.neeuro.FaceView – Richelle Chia Dec 20 '17 at 09:23

0 Answers0