ImageView Visibility Timer Error

package name.cpr; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import java.util.Timer; import java.util.TimerTask; public class ExampleActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_example); Timer timer = new Timer(); timer.schedule(new CheckConnection(), 0, 3000); ImageView iv = (ImageView) findViewById(R.id.imageView); iv.setVisibility(View.VISIBLE); } class CheckConnection extends TimerTask{ public void run(){ ImageView iv = (ImageView) findViewById(R.id.imageView); iv.setVisibility(View.INVISIBLE); //<- Unfortunatly Error Here } } } 

the beginning of the application, the visibility of the first time visibility, but does not work with the timer if the timer launched the same error Unfortunately .... stopped

+6
source share
2 answers

You can use android.os.Handler instead.

 public class ExampleActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_example); ImageView iv = (ImageView) findViewById(R.id.imageView); iv.setVisibility(View.VISIBLE); // new Handler().postDelayed(new Runnable() { @Override public void run() { iv.setVisibility(View.INVISIBLE); } }, 3000); } } 

Good luck. :)

0
source

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); } /** * The timer task to run every 3 seconds */ private final class CheckConnection extends TimerTask { public void run() { handler.sendEmptyMessage(0); } } /** * Custom <b>static</b> handler to bridge between the timer task thread and the gui */ private static final class ImgHandler extends Handler { // use a weak reference to the ImageView instance you'd like to manipulate: private final WeakReference<ImageView> imageReference; public ImgHandler(final ImageView img) { this.imageReference = new WeakReference<ImageView>(img); } @Override public void handleMessage(Message msg) { if (imageReference == null) return; // no image to show / hide final ImageView img = imageReference.get(); // since this is the single message we are handling, no switch is used: if (img != null) { img.setVisibility(img.getVisibility() == View.VISIBLE ? View.INVISIBLE : View.VISIBLE); } } } } 

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.

0
source

Source: https://habr.com/ru/post/979686/


All Articles