0

I have a Video Player using library and On him ImageView, ie the type of banner that covers 30% of the video player. Every 15 seconds, the picture should change.

mBannerView.setImageBitmap (BitmapFactory.decodeFile (file.getAbsolutePath ()));
Thread.sleep (1000 * 15);

But I have a crash when changing the picture with the message:

Android "Only the original thread that created a view hierarchy can Touch its views. "

I tried to write these lines to UI stream:

    

runOnUiThread (new Runnable () {
        @Override
        public void run () {
            ............
        }
    });

ImageView but then does not respond to any timer or change.

I think right now (maybe wrong, in this case, please correct) that the focus is on the video player as the video plays, and so the picture above it is changed, crashes down.

If so, how in that case, how not to lose focus with ImageView while and continued to play a video? And if not, what could be wrong?

UPD: code:

 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();
        }
    }
Nurlan Kanimetov
  • 521
  • 1
  • 4
  • 4
  • Possible duplicate of [Android "Only the original thread that created a view hierarchy can touch its views."](http://stackoverflow.com/questions/5161951/android-only-the-original-thread-that-created-a-view-hierarchy-can-touch-its-vi) – Code-Apprentice Feb 14 '17 at 00:50
  • @Code-Apprentice, I wrote that tried runOnUiThread// – Nurlan Kanimetov Feb 14 '17 at 00:59
  • Android's concept of "focus" is different than what you are familiar with on desktop computers. I doubt the issue has anything to do with "focus". – Code-Apprentice Feb 14 '17 at 01:04

1 Answers1

1

This does nothing with the focus. You MUST NOT touch UI widgets from other threads than UI thread. And there's no exception. runOnUiThread() should come as solution as you guessed but while you show no code that you run it's hard to say what you did wrong there.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141