为什么处理程序中的文本没有更新?

时间:2012-07-18 06:57:19

标签: android textview

我正在尝试更新两个TextViews txtName和textEndName。当我调试有时它会更新文本,但其余的时间它不起作用。有时它可以工作,但并不完全是它的假设。下面给出的代码中的问题是什么?

    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 个答案:

答案 0 :(得分:0)

你不应该这样做:

handler.dispatchMessage(handler.obtainMessage());

请改为尝试:

handler.sendMessage(handler.obtainMessage());

编辑:添加了我注意到的其他内容

我也看到你的主题中有一个循环while (isDownloading),你在isDownloading块中将false设置为finally。这个循环从不运行多次。

另外,你做了

btnplay.setImageResource(R.drawable.play);

在thead里面。这不好。您需要在主(UI)线程上执行与UI相关的所有操作。