ads

jeudi 24 septembre 2015

[Q] How do I fix my issue with an infinite timer?



Hi, I am trying to run a small app which is supposed to show a progress bar for appx. 3 seconds and then make it disappear. Currently the app starts the progress bar but it never stops and I'm not sure why as I don't see anything wrong with the code. Any help is appreciated @mmdeveloper10


Code:


import android.app.*;
import android.os.*;
import android.view.View;
import android.widget.Button;
import android.widget.*;
import java.util.*;

public class MainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
       
        int x = 1;
       
        public void save(View v){
                startTimer();
        }
       
        public void startTimer(){
                Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                                @Override
                                public void run() {
                                        runOnUiThread(new Runnable() {

                                                        @Override
                                                        public void run() {
                                                                if(x==1){
                                                                        x++;
                                                                        ProgressBar circle = (ProgressBar) findViewById(R.id.circle1);
                                                                        circle.setVisibility(View.VISIBLE);
                                                                        Button button = (Button) findViewById(R.id.button1);
                                                                        button.setText("Saving...");

                                                                }
                                                                else if (x >= 3){
                                                                        x++;
                                                                        ProgressBar circle = (ProgressBar) findViewById(R.id.circle1);
                                                                        circle.setVisibility(View.INVISIBLE);
                                                                        Button button = (Button) findViewById(R.id.button1);
                                                                        button.setText("World Saved.");

                                                                }
                                                        }
                                                });
                                }
                        },0,1000);
        }
}





Aucun commentaire:

Enregistrer un commentaire