Reputation: 444
The code below is going to act as a Timer. I was testing out the AsynchTask, when my app simply doesn't load the XML view. My code should work, but the view is not opening up. I later did some testing and moves the TextView variable "display" around a little and eventually got the view to show up, but the start button press didn't work... Can anyone tell me how I could get the TextView to update and run properly? Thanks!
THE ENTIRE LAYOUT IS NOT DISPLAYING....
public class MainActivity extends Activity {
timer tim = new timer();
boolean running = true;
int milli = 0;
int secs = 0;
int mins = 0;
int hours = 0;
ArrayList<Lap> laps = new ArrayList<Lap>();
TextView display = (TextView) findViewById(R.id.textView1); //whats going on?
LinearLayout lapsView = (LinearLayout) findViewById(R.id.lapsView);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
Button b1 = (Button) findViewById(R.id.start);
b1.setOnClickListener(startTimer);
Button b2 = (Button) findViewById(R.id.stop);
b2.setOnClickListener(stopTimer);
}
catch(Exception e1){
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private OnClickListener startTimer = new OnClickListener() {
public void onClick(View v) {
if(!running){
startWatch();
}
else if(running){
try{
tim.execute(""); //start the task
}
catch(Exception e1){
Toast.makeText(MainActivity.this, "Error starting: "+e1, Toast.LENGTH_LONG).show();
}
}
}
};
private OnClickListener stopTimer = new OnClickListener() {
public void onClick(View v) {
if(running){
stopWatch();
}
}
};
public void startWatch(){
running = true;
}
public void stopWatch(){
running= false;
}
public class timer extends AsyncTask<String, Void, String> {
////NOT WORKING!!!!
@Override
protected String doInBackground(String... params) {
while(true){
while(!running){
///Do Nothing...
}
while(milli < 1000 && running){
running = true;
milli++;
try{
Thread.sleep(1);
}
catch(Exception e1){
System.out.println(e1);
}
if(milli == 1000){
milli = 0;
secs++;
}
if(secs == 60){
secs = 0;
mins++;
}
if(mins == 60){
hours++;
mins = 0;
}
display.setText(milli+":"+secs+":"+mins+":"+hours);
}
}
}
}
}
Debug Errors....
Thread [<1> main] (Suspended (exception RuntimeException))
<VM does not provide monitor information>
ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2053
ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2154
ActivityThread.access$700(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 146
ActivityThread$H.handleMessage(Message) line: 1260
ActivityThread$H(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 137
ActivityThread.main(String[]) line: 4949
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 511
ZygoteInit$MethodAndArgsCaller.run() line: 1043
ZygoteInit.main(String[]) line: 810
NativeStart.main(String[]) line: not available [native method]
Upvotes: 2
Views: 7695
Reputation: 21072
You asyncTask can't update the UI on the doInBackground
method. To do this, you'll call publishProgress
from doInBackground
and update the TextView
there.
public class timer extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
while(true){
while(milli < 1000 && running){
running = true;
milli++;
try{
Thread.sleep(1);
}
catch(Exception e1){
System.out.println(e1);
}
if(milli == 1000){
milli = 0;
secs++;
}
if(secs == 60){
secs = 0;
mins++;
}
if(mins == 60){
hours++;
mins = 0;
}
this.publishProgress(milli+":"+secs+":"+mins+":"+hours);
}
}
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
TextView display = (TextView) findViewById(R.id.textView1);
display.setText(values[0]);
}
}
Upvotes: 6