简体   繁体   中英

TextView widget not updating on android2.2

I made a media player that streams through shoutcast and I was trying to get the song title off that stream. I have successfully got the song title and made this display on a textview and I update it when the song changes.

I have tested this code with android emulator, using android 1.5 and android 2.1. The code works but when I tested it on android 2.2 the text is not updating.

Any ideas why it is not updating? The code I used to update the textview is below.

when i click on the button in my layout pageArtist() and pageTitle() will be called and they will get the song title and artist and change the text of the textview every 4seconds

MY CODE IN MY ONCREATE()

public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        streamButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
                    pageArtist();
        pageTitle();
    }

HERE'S MY METHOD FOR UPDATING MY TEXTVIEW

    private void pageTitle(){
    timer = new Timer();
    timer.schedule(new TimerTask() {

        public void run() {

            URL url;

           try {
                  url = new URL("http://teststream.com:8000/listen.mp3");
                  IcyStreamMeta icy = new IcyStreamMeta(url);

                  TextView title1 = (TextView) findViewById(R.id.song_title);
                  String T;
                  T = icy.getTitle();
                  title1.setText(T);

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }, 2, 4000); 
   }

EDIT : THIS CODE GETS REPEATED EVERY 4 SECOND WITH TIMER

The comment by Adil is correct. You can't update the UI from a background thread, only the UI thread. To do this via the ui thread, remove the code in the body of your run method that sets the text and instead use a Handler:

private Handler handler;

public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        handler = new Handler();
        streamButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
               pageArtist();
               pageTitle();
           }
       }
  }

 private void pageTitle(){
    timer = new Timer();
    timer.schedule(new TimerTask() {

        public void run() {

            URL url;

           try {
                  url = new URL("http://teststream.com:8000/listen.mp3");
                  IcyStreamMeta icy = new IcyStreamMeta(url);

                  final TextView title1 = (TextView) findViewById(R.id.song_title);
                  final String T = icy.getTitle();
                  handler.post(new Runnable(){
                      public void run(){
                          title1.setText(T);
                      }
                   });
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }, 2, 4000); 
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM