0

I am trying to update two TextViews txtName and textEndName. When i debug sometimes it updates the text but rest of the time it does not works. Sometime it works but not not exactly what it is suppose to.What is the problem in the below given code ?

    txtName = (TextView) findViewById(R.id.txtstarttag);
    textEndName = (TextView) findViewById(R.id.txtendtag);
    startSec = (TextView) findViewById(R.id.txtstartsecnd);
    endSec = (TextView) findViewById(R.id.txtendsecnd);
    btnplay = (ImageButton) findViewById(R.id.btnplay);
    btnback = (Button) findViewById(R.id.btnback);
    btnback.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            ((MyMixes) getParent()).goBack();
        }
    });

    btnplay.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            btnClick();
        }
    });

    splitArray();

    Thread t = new Thread()
    {
        public void run() 
        {
            while (isDownloading) 
            {
                if (Data.filenames != null && Data.filenames.size() >= (getIntent() .getExtras().getInt("index") + 1) && Data.filenames.get(getIntent().getExtras() .getInt("index")) != null) 
                {
                    try 
                    {
                        mediaPlayer = new MediaPlayer();
                        mediaPlayer.setDataSource(Data.filenames.get(getIntent().getExtras().getInt("index")));
                        mediaPlayer.prepare();
                        mediaPlayer.start();
                        Timer t = new Timer();

                        handler.postDelayed(onEverySecond, 1000);
                        mediaPlayer.setOnCompletionListener(new OnCompletionListener() 
                        {
                            @Override
                            public void onCompletion(MediaPlayer mp) {
                                mediaPlayer.seekTo(0);
                            }
                        });
                        boolean isPlaying = mediaPlayer.isPlaying();
                        if(!isPlaying){
                            mediaPlayer.pause();

                        }
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        isDownloading = false;
                    }
                }
            }
        }
    };
    t.start();
}

private Runnable onEverySecond=new Runnable()
{
    public void run() 
    {
        if (mediaPlayer.isPlaying()) 
        {
            for (int i = index; i < intervals.length; i++) 
            {
                if (i != index && mediaPlayer.getCurrentPosition() > intervals[i]) 
                {
                    index = i;
                    handler.dispatchMessage(handler.obtainMessage());
                    handler.postDelayed(onEverySecond, 1000);
                    break;
                }
            }
        }
    }
};

Handler handler = new Handler() 
{
    @Override
    public void handleMessage(Message msg) {
        txtName.setText(arrName.get(index));
        textEndName.setText(arrName.get(index+1));
        super.handleMessage(msg);
    }
};

public void splitArray()
{

    strSplit = kggg.split("\n");// string array
    intervals = new long[strSplit.length];
    Time timer;

    for (int j = 0; j < strSplit.length; j++) 
    {
        split = strSplit[j];// string
        split1 = split.split("-");// string array

        arrName.add(split1[0]);
        String timeq = split1[1];

        String[] timed = timeq.split(":");

        if(timed.length == 3)
        {
            timer = new Time(Integer.parseInt(timed[0].trim()), Integer.parseInt(timed[1].trim()), Integer.parseInt(timed[2].trim()));
            intervals[j] = timer.getTime();
        }
        else if(timed.length == 2)
        {
            timer = new Time(0, Integer.parseInt(timed[0].trim()), Integer.parseInt(timed[1].trim()));
            intervals[j] = timer.getTime();
        }
        else if(timed.length == 1)
        {
            timer = new Time(0, 0, Integer.parseInt(timed[0].trim()));
            intervals[j] = timer.getTime();
        }
    }

    for (int j = intervals.length-1; j >= 0 ; j--) 
    {
        intervals[j] = intervals[j] - intervals[0]; 
    }
}
public void btnClick()
{
    k++;
    k = k % 2;
    startSong(k);
}
private void startSong(int i) {
    if (i == 1) {
        System.out.println("11111" + i);
        btnplay.setImageResource(R.drawable.pause);
        try {
            System.out.println("start try chech------");
            mediaPlayer.start();
        } catch (Exception e) {
            mediaPlayer.pause();
        }
    }
    if (i == 0) {
        btnplay.setImageResource(R.drawable.play);
        mediaPlayer.pause();
        System.out.println("00000" + i);
    }
}

1 Answer 1

0

You shouldn't be doing this:

handler.dispatchMessage(handler.obtainMessage());

Try this instead:

handler.sendMessage(handler.obtainMessage());

EDIT: Added other things I noticed

I also see that in your Thread you have a loop while (isDownloading) and you set isDownloading to false in the finally block. This loop while never run more than once.

Also, you do

btnplay.setImageResource(R.drawable.play);

inside a thead. This isn't good. You need to do everything related to UI on the main (UI) thread.

4
  • please add some debug logging so that you can tell if the Runnable is getting run multiple times and also if the handleMessage() method is being called Jul 19, 2012 at 12:54
  • When i am debugging then everything works fine but when i run the same piece of code the texts are not updated. Please suggest what to do ? I am using timer inside a thread, is anything to do with that ?
    – bkshukla
    Jul 19, 2012 at 13:06
  • Add debug logging, run it on a real device and look at the logcat. What do you mean you are using a timer inside a thread. The code you've posted looks OK. Jul 19, 2012 at 13:19
  • I added some other observations to my answer. Jul 19, 2012 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.