Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 998009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:07:36+00:00 2026-05-16T07:07:36+00:00

When using this class I get this strange exception. It is used to print

  • 0

When using this class I get this strange exception. It is used to print out a nice timer display for my game, and is created when my main GameView class is created. The error gets thrown at line 26: super(s*1000,1000);

package tommedley.android.game;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.CountDownTimer;

public class Timer extends CountDownTimer{
    public static int MODE_COUNTING = 0;
    public static int MODE_PAUSED = 1;
    public static int MODE_FINISHED = 2;

    private Canvas canvas;
    private Context context;
    private float xPos;
    private float yPos;
    private static int DIGIT_WIDTH = 50;
    private static int DIGIT_HEIGHT = 70;
    private int numDigits = 3;
    private int seconds;
    Drawable[] digitImgs;
    private int mode;


    public Timer(Canvas c, Context con, float x, float y, int digits, int s){
        super(s*1000, 1000);
        mode = MODE_COUNTING;
        canvas = c;
        context = con;
        xPos = x;
        yPos = y;
        seconds = s;
        numDigits = digits;
        digitImgs = new Drawable[numDigits];
        this.start();
    }
    public void draw(){
        String reprNum = String.format("%0"+numDigits+"d", seconds);
        for(int i = 0;i<numDigits;i++){
            switch(reprNum.charAt(i)){
            case '0':
                digitImgs[i] = context.getResources().getDrawable(R.drawable.digit_0);
                break;
            case '1':
                digitImgs[i] = context.getResources().getDrawable(R.drawable.digit_1);
                break;
            case '2':
                digitImgs[i] = context.getResources().getDrawable(R.drawable.digit_2);
                break;
            case '3':
                digitImgs[i] = context.getResources().getDrawable(R.drawable.digit_3);
                break;
            case '4':
                digitImgs[i] = context.getResources().getDrawable(R.drawable.digit_4);
                break;
            case '5':
                digitImgs[i] = context.getResources().getDrawable(R.drawable.digit_5);
                break;
            case '6':
                digitImgs[i] = context.getResources().getDrawable(R.drawable.digit_6);
                break;
            case '7':
                digitImgs[i] = context.getResources().getDrawable(R.drawable.digit_7);
                break;
            case '8':
                digitImgs[i] = context.getResources().getDrawable(R.drawable.digit_8);
                break;
            case '9':
                digitImgs[i] = context.getResources().getDrawable(R.drawable.digit_9);
                break;
            }
            digitImgs[i].setBounds((int)xPos+DIGIT_WIDTH*i, (int)yPos, (int)xPos+DIGIT_WIDTH*(i+1), (int)yPos+DIGIT_HEIGHT);
            digitImgs[i].draw(canvas);
        }
    }
    @Override
    public void onFinish() {
        seconds = 0;
        mode = MODE_FINISHED;
    }
    @Override
    public void onTick(long millsLeft) {
        seconds = (int)millsLeft / 1000;
    }
    public int getMode(){
        return mode;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:07:36+00:00Added an answer on May 16, 2026 at 7:07 am

    That is a bit strange — I think you must be constructing this object on a thread other than the main thread?

    I don’t know if you’re familiar with Handlers and Loopers, but they are Android’s way of doing asynchronous operations on a thread. If you want to execute asynchronous operations on a thread, you use a Handler to do it. But before you can use a Handler you must prep the thread by creating a MessageQueue, which is done by calling Looper.prepare().

    If you look at the source for the CountDownTimer class you’ll notice that it creates a private Handler instance. This member variable is created directly before the constructor executes, which is why the exception is being thrown at the call to super.

    So, all you need to do is call Looper.prepare() before you construct the Timer. However, you do not need to do this if you construct the Timer on the main thread. The main thread of an Android process is automatically declared to be a looper (if you are already calling this on the main thread and are still getting this exception, then that is very strange indeed). As far as I can tell from the sparse javadocs, this class was meant to be used on the main thread, so that’s what I would recommend doing. Your onTick method seems light enough that it’s not worth the overhead of an extra thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.