Reputation: 3
I'm trying to make a timer for the android app I'm developing and am trying to make a test timer that will display the time left. I have tried a lot of websites but none work. Here is the complete code from the activity:
package com.hunter.app;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class Settings extends Activity {
//intent to return to menu from settings
public void back (View view) {
Intent back = new Intent(this, MainActivity.class);
startActivity(back);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
//calls on textview
final TextView text1 = (TextView)findViewById(R.id.textView1);
//length of timer
new CountDownTimer(30000, 1000) {
//counts the milliseconds
public void onTick(long millisUntilFinished) {
//displays seconds remaining
text1.setText("seconds remaining: " + millisUntilFinished / 1000);
}
//displays when timer finishes
public void onFinish() {
text1.setText("done!");
} //<================================== error
this.start();
} //<============================== error
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.settings, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Upvotes: 0
Views: 175
Reputation: 12527
The "start" call needs to be made on the object instance as follows:
(new CountDownTimer(30000, 1000) {
// your code here..
}).start();
In you code, the "this.start()" is within the class definition. It is not within any method.
Note that the "(" and ")" I have included are optional. I prefer to add them for clarity so that the object instance itself is contained. You could however do the following:
new CountDownTimer(30000, 1000) {
// your code here...
}.start();
Upvotes: 1