What you are doing is updating the gui element from a non-gui thread that ends with a RuntimeException
. One way to fix this is to use the Handler
:
public class ExampleActivity extends Activity { private ImageView iv; private ImgHandler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_example); this.iv = (ImageView) findViewById(R.id.imageView); this.iv.setVisibility(View.VISIBLE); this.handler = new ImgHandler(this.iv); final Timer timer = new Timer(); timer.schedule(new CheckConnection(), 0, 3000); } private final class CheckConnection extends TimerTask { public void run() { handler.sendEmptyMessage(0); } } private static final class ImgHandler extends Handler {
The above example will change the visibility of your imageView every 3 seconds, although it is not clear from your example whether this was your intention.
source share