0

我在一个活动中编写了一个类,以在我的活动中显示一个 hh:mm:ss 计时器:

这是旨在显示计时器的类的代码:

//class to display on screen timer
       class ShowTimer
       {
           long mMilliseconds = 120000;
            SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat("HH:mm:ss");


            CountDownTimer mCountDownTimer = new CountDownTimer(mMilliseconds, 1000) {
                @Override
                public void onFinish() {
                    mTextView.setText(mSimpleDateFormat.format(0));
                }

                public void onTick(long millisUntilFinished) {
                    mTextView.setText(mSimpleDateFormat.format(millisUntilFinished));
                }
            };
       }

这就是我尝试访问课程的方式:

mSimpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
            mTextView = (TextView) findViewById(R.id.timer_textView);

            mCountDownTimer.start();

我收到这样的错误:

mCountDownTimer cannot be resolved

完整的活动在这里:

public class SpeedTestExamActivity extends Activity {

    String xmlContent=null;
    String duration=null;
    //strings for use
        String answer_str, option1_str,option2_str,option3_str,option4_str;
        //text views for quiz layout
        TextView question_view;
        TextView question_sr_no;
        RadioButton option1;
        RadioButton option2;
        RadioButton option3;
        RadioButton option4;
        int counter=0;
        int loop_checker=0;
        int i;

        //buttons on UI
        Button prevQuestion;
        Button nextQuestion;
        Button resetQuestion;
        Button endTest;
        // XML node keys
                static final String KEY_LIST = "List"; // parent node
                static final String KEY_SR_NO = "SRNo";
                static final String KEY_EXAM_SET_ID="ExamSetId";
                static final String KEY_Q_ID="QId";
                static final String KEY_QT_ID="QTId";
                static final String KEY_QUESTION = "Question";
                static final String KEY_MARKS = "Marks";
                static final String KEY_NEGATIVE_MARKS = "NegativeMark";
                static final String KEY_ATTEMPTED_TIME = "AttemtedTime";
                static final String KEY_IDLE_TIME = "IdleTime";
                static final String KEY_ELAPSED_TIME = "ElapsedTime";
                static final String KEY_LAST_Q_INDEX = "LastQIndex";
                static final String KEY_SUBJECT_NAME = "SubjectName";
                static final String KEY_OPTION1 = "Option1";
                static final String KEY_OPTION2 = "Option2";
                static final String KEY_OPTION3 = "Option3";
                static final String KEY_OPTION4 = "Option4";
                static final String KEY_CORRECT = "Correct";

                //variables to run the timer
                int test_duration; 

                //object of the handler class
                Handler handler;

                //textview for timer
                TextView mTextView;
                SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat("HH:mm:ss");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_speed_test_exam);

        //getting previously received content as xml
        xmlContent=getFromPreference("SpeedTestContent");

        //getting previously saved exam duration from preference
        duration=getFromPreference("exam_duration");

        //setting time for timer to finish activity after test duration is over
        test_duration=Integer.parseInt(duration.toString())*60000;

        //makeAToast("Test duration is ms: "+test_duration);


         mSimpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
            mTextView = (TextView) findViewById(R.id.timer_textView);

            mCountDownTimer.start();

        //starting timer
        runTimer();




        //assigning objects to layouts
         question_view=(TextView)findViewById(R.id.question_textView);
         question_sr_no=(TextView)findViewById(R.id.question_id_textView);
        option1=(RadioButton)findViewById(R.id.option1_radioButton);
        option2=(RadioButton)findViewById(R.id.option2_radioButton);
        option3=(RadioButton)findViewById(R.id.option3_radioButton);
        option4=(RadioButton)findViewById(R.id.option4_radioButton);

        //calling function to populate ui
        populating_textview(counter);

        //onclick opt 1
        option1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                option2.setChecked(false);
                option3.setChecked(false);
                option4.setChecked(false);
               //getting text from TextView and checking whether it is equal to ans
                option1_str=option1.getText().toString();
                if (answer_str.equalsIgnoreCase(option1_str))
                {

                    //makeAToast("Correct!");


                    //calling function to populate ui with next question
//              counter++;
//              if (counter<=loop_checker)
//              {
//              populating_textview(counter);
//              }
//              else
//              {
//                  makeAToast("Game Over!");
//              }
                }
                else
                {
                    //makeAToast("Wrong answer!");
                }

            }
        });


        //onclick opt 2
        option2.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                option1.setChecked(false);
                option3.setChecked(false);
                option4.setChecked(false);

                //getting text from TextView and checking whether it is equal to ans
                option2_str=option2.getText().toString();
                String answer=answer_str;

                if (answer.equalsIgnoreCase(option2_str))
                {
                    //calling function to populate ui with next question
                //makeAToast("Correct!");   


//              counter++;
//              if (counter<=loop_checker)
//              {
//              populating_textview(counter);
//              }
//              else
//              {
//                  makeAToast("Game Over!");
//              }
                }
                else
                {
                    //makeAToast("Wrong answer!");
                }

            }
        });


        //onclick opt 3                 
     option3.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                option1.setChecked(false);
                option2.setChecked(false);
                option4.setChecked(false);
                //getting text from TextView and checking whether it is equal to ans
                option3_str=option3.getText().toString();
                String answer1=answer_str;

                if (answer1.equalsIgnoreCase(option3_str))
                {
                    //calling function to populate ui with next question
                //makeAToast("Correct!");


//              counter++;
//              if (counter<=loop_checker)
//              {
//              populating_textview(counter);
//              }
//              else
//              {
//                  makeAToast("Game Over!");
//              }
                }
                else
                {
                    //makeAToast("Wrong answer!");
                }

            }
        });

   //onclick opt 4
     option4.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                option1.setChecked(false);
                option2.setChecked(false);
                option3.setChecked(false);
                //getting text from TextView and checking whether it is equal to ans
                option4_str=option4.getText().toString();
                String answer2=answer_str;

                if (answer2.equalsIgnoreCase(option4_str))
                {
                    //calling function to populate ui with next question
                //makeAToast("Correct!");   


//              counter++;
//              if (counter<=loop_checker)
//              {
//              populating_textview(counter);
//              }
//              else
//              {
//                  makeAToast("Game Over!");
//              }
                }
                else
                {
                    //makeAToast("Wrong answer!");
                }

            }
        });


     //onclick next button
     nextQuestion = (Button) findViewById(R.id.next_question_button);
     nextQuestion.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                option1.setChecked(false);
                option2.setChecked(false);
                option3.setChecked(false);
                option4.setChecked(false);
                counter++;
                if (counter<=loop_checker)
                {
                populating_textview(counter);
                }
                else
                {
                    makeAToast("Game Over!");
                }
            }
        });

   //onclick previous button
     prevQuestion = (Button) findViewById(R.id.previous_question_button);
     prevQuestion.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if(counter!=0)
                {
                    counter--;
                }

                if (counter<loop_checker)
                {

                populating_textview(counter);
                }
                 if(counter==loop_checker)
                {
                    counter--;
                    populating_textview(counter);
                }
                 if(counter==0)
                 {
                     makeAToast("No more questions!");
                 }

            }
        });
   //onclick reset button
     resetQuestion = (Button) findViewById(R.id.reset_question_button);
     resetQuestion.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                option1.setChecked(false);
                option2.setChecked(false);
                option3.setChecked(false);
                option4.setChecked(false);

            }
        });
   //onclick end test button
     endTest = (Button) findViewById(R.id.end_test_button);
     endTest.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //deactivating timer before finishing activity
                handler.removeCallbacksAndMessages(null);
                finish();
                Intent intent = new Intent(SpeedTestExamActivity.this, RateUsActivity.class);
                SpeedTestExamActivity.this.startActivity(intent);               
            }
        });
    }

    //deactivating back button
    @Override
    public void onBackPressed() {

    }   


    //getting content from preferences
    public String getFromPreference(String variable_name)
    {
        String get_content;
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        get_content = preferences.getString(variable_name,"");

        return get_content;
       // makeAToast(xmlContent);
    }

    // function to populate ui with question counter
            void populating_textview(int count_questions)
            {

                ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

                XMLParser parser = new XMLParser();
                //String xml = parser.getXmlFromUrl(URL); // getting XML
                Document doc = parser.getDomElement(xmlContent); // getting DOM element

                //count_questions=2;

                NodeList nl = doc.getElementsByTagName(KEY_LIST);
                // looping through all item nodes <item>
                for ( i = 0; i < nl.getLength();i++) {

                    loop_checker=i;
//              while(counter< nl.getLength())
//              {
                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    Element e = (Element) nl.item(count_questions);
                    // adding each child node to HashMap key => value
                    map.put(KEY_LIST, parser.getValue(e, KEY_LIST));
                    map.put(KEY_SR_NO, parser.getValue(e, KEY_SR_NO));
                    question_sr_no.setText(parser.getValue(e, KEY_SR_NO)+".");


                    map.put(KEY_QUESTION, parser.getValue(e, KEY_QUESTION));                
                    question_view.setText(parser.getValue(e, KEY_QUESTION));


                    map.put(KEY_OPTION1, parser.getValue(e, KEY_OPTION1));
                    //option1_str =parser.getValue(e, KEY_OPTION1);
                    option1.setText(parser.getValue(e, KEY_OPTION1));


                    map.put(KEY_OPTION2, parser.getValue(e, KEY_OPTION2));
                    option2.setText(parser.getValue(e, KEY_OPTION2));
                    //option2_str =parser.getValue(e, KEY_OPTION2);


                    map.put(KEY_OPTION3, parser.getValue(e, KEY_OPTION3));
                    option3.setText(parser.getValue(e, KEY_OPTION3));
                    //option3_str =parser.getValue(e, KEY_OPTION3);


                    map.put(KEY_OPTION4, parser.getValue(e, KEY_OPTION4));
                    option4.setText(parser.getValue(e, KEY_OPTION4));
                    //option4_str =parser.getValue(e, KEY_OPTION4);


                    map.put(KEY_CORRECT,  parser.getValue(e, KEY_CORRECT));
//                  makeAToast(parser.getValue(e, KEY_ANSWER));
                    answer_str =parser.getValue(e, KEY_CORRECT);
                    // adding HashList to ArrayList
                    menuItems.add(map);
                }
            }

    //method to run timer
            public void runTimer()
            {
                 handler = new Handler();
                 // run a thread after a particular time seconds to start the home screen
                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        makeAToast("Your time is up!");

                        Intent intent = new Intent(SpeedTestExamActivity.this, RateUsActivity.class);
                        finish();
                        SpeedTestExamActivity.this.startActivity(intent);
                   }

                }, test_duration); // time in milliseconds (1 second = 1000 milliseconds) until the run() method will be called
            }
    //method to show toast
        public void makeAToast(String str) {
            //yet to implement
            Toast toast = Toast.makeText(this,str, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }

        //class to display on screen timer
       class ShowTimer
       {
           long mMilliseconds = 120000;
            SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat("HH:mm:ss");


            CountDownTimer mCountDownTimer = new CountDownTimer(mMilliseconds, 1000) {
                @Override
                public void onFinish() {
                    mTextView.setText(mSimpleDateFormat.format(0));
                }

                public void onTick(long millisUntilFinished) {
                    mTextView.setText(mSimpleDateFormat.format(millisUntilFinished));
                }
            };
       }
}

问题是,我已经有了毫秒的时间,我不能使用任何硬编码值。

我已按照此处的教程进行操作。

我哪里错了?我应该怎么做才能实现计时器?

提前致谢

4

3 回答 3

1

Try this:

ShowTimer showTimer = new ShowTimer();
showTimer.mCountDownTimer.start();

Or better use an accessor in ShowTimer:

showTimer.getCountDownTimer().start();

The getCountDownTimer returns mCountDownTimer:

public CountDownTimer getCountDownTimer() {
    return mCountDownTimer;
}
于 2013-03-18T12:38:02.160 回答
0

您已经mCountDownTimer在内部类而不是主类中声明。如果您将其移至您的SpeedTestExamActivity班级,您将能够访问它。

于 2013-03-18T12:33:57.633 回答
0

mCountDownTime是一个实例字段,因此您需要一个实例ShowTimer来访问该字段。

于 2013-03-18T12:35:47.220 回答