java如何不失去焦点?
我有一个使用library的视频播放器,他身上有ImageView
,即覆盖视频播放器30%的横幅类型。每15秒,图片就会改变
mBannerView.setImageBitmap (BitmapFactory.decodeFile (file.getAbsolutePath ()));
Thread.sleep (1000 * 15);
但我在更改带有以下消息的图片时发生崩溃:
Android "Only the original thread that created a view hierarchy can Touch its views. "
我试图将这些行写入UI
流:
runOnUiThread (new Runnable () {
@Override
public void run () {
............
}
});
ImageView
但不响应任何计时器或更改
我认为现在(可能是错误的,在这种情况下,请更正)在视频播放时,焦点集中在视频播放器上,因此上面的图片发生了变化,崩溃了
如果是,那怎么办?, 如何在继续播放视频的同时不失去焦点? 如果不是,那又有什么问题呢
UPD:代码:
public void bannerSlide(boolean run) {
Timer timer = new Timer();
File file;
if (run){
mBannerView.setVisibility(View.VISIBLE);
final List<Banner> bannerList = GlobalData.loadBannerInfo(MainActivity.this);
if (bannerList != null && bannerList.size() > 0){
for (int i = 0; i < bannerList.size(); i++){
file = new File(Application.banner_path + bannerList.get(i).getName());
if (file.exists()){
mBannerView.setImageBitmap(BitmapFactory.decodeFile(file.getAbsolutePath()));
}
final int finalI = i;
timer.schedule(new TimerTask() {
@Override
public void run() {
if (finalI == bannerList.size() - 1){
bannerSlide(true);
}
}
}, 15 * 1000);
}
} else {
mBannerView.setVisibility(View.GONE);
}
} else {
mBannerView.setVisibility(View.GONE);
timer.cancel();
}
}
# 1 楼答案
这与焦点无关。您不能从UI线程以外的其他线程触摸UI小部件。也不例外
runOnUiThread()
应该如您所猜测的那样成为解决方案,但尽管您没有显示您运行的代码,但很难说您在那里做错了什么