TextView小部件不在android2.2上更新

时间:2011-12-02 17:10:05

标签: android android-widget

我制作了一个媒体播放器,通过shoutcast传播,我试图从该流中获得歌曲标题。我已成功获得歌曲标题,并在textview上显示,并在歌曲改变时更新。

我已经使用Android 1.5和Android 2.1测试了这个代码与android模拟器。代码有效,但是当我在android 2.2上测试时,文本没有更新。

为什么它不更新的任何想法?我用来更新textview的代码如下。

当我点击我的布局页面中的按钮时,艺术家()和pageTitle()将被调用,他们将获得歌曲标题和艺术家,并每隔4秒更改一次文本视图的文本

我的代码中的我的代码()

public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        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);

                  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); 
   }

编辑:此代码每次使用TIMER重复4次

1 个答案:

答案 0 :(得分:1)

Adil的评论是正确的。您无法从后台线程更新UI,只能更新UI线程。要通过ui线程执行此操作,请删除设置文本的run方法正文中的代码,而不是使用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); 
   }
相关问题