6
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
        }
    }
}

starting app, first time image view visibility work but with timer not working, if timer started same error Unfortunately .... has stopped

gio
  • 4,950
  • 3
  • 32
  • 46

2 Answers2

0

You may want to 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. :)

Hemendra Sharma
  • 1,063
  • 9
  • 21
  • 3
    This is the correct approach: using a `Handler`. This code will wait 3 seconds, and then hide the image; the op's code hides the image instantly, than waits 3 secs and hides it again.. and again. – rekaszeru Dec 16 '14 at 13:45
0

What you are doing is updating a gui element from a non-gui thread, which ends up in a RuntimeException. One way to fix it is to use a 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's 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 example above will change the visibility of your imageView every 3 seconds, though it's not clear from your example whether that was your intent.

rekaszeru
  • 19,130
  • 7
  • 59
  • 73