gpt4 book ai didi

java - 一个类的方法没有被另一个类调用

转载 作者:行者123 更新时间:2023-12-01 14:45:22 25 4
gpt4 key购买 nike

我在一个 Activity 中编写了一个类,以在我的 Activity 中显示 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

完整 Activity 在这里:

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

问题是,我已经有了以毫秒为单位的时间,我无法使用任何硬编码值。

我已按照here中的教程进行操作.

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

提前致谢

最佳答案

试试这个:

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

或者更好地在 ShowTimer 中使用访问器:

showTimer.getCountDownTimer().start();

getCountDownTimer 返回 mCountDownTimer:

public CountDownTimer getCountDownTimer() {
return mCountDownTimer;
}

关于java - 一个类的方法没有被另一个类调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15476881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com