【问题标题】:ImageView visibility Error with Timer计时器的 ImageView 可见性错误
【发布时间】:2015-02-14 13:18:10
【问题描述】:
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
        }
    }
}

启动应用程序,第一次图像视图可见性工作,但计时器不工作,如果计时器启动同样的错误不幸......已停止

【问题讨论】:

  • 您可能想要编辑您的问题伙伴。添加错误日志并添加 Android 标记 :)
  • 请提供完整的错误表单 logcat

标签: android timer imageview visibility timertask


【解决方案1】:

您可能想改用 android.os.Handler。

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);
    }
}

祝你好运。 :)

【讨论】:

  • 这是正确的方法:使用Handler。此代码将等待 3 秒,然后隐藏图像;操作的代码会立即隐藏图像,然后等待 3 秒并再次隐藏它.. 一次又一次。
【解决方案2】:

您正在做的是从非 gui 线程更新 gui 元素,该线程最终以 RuntimeException 结尾。 解决它的一种方法是使用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);
            }
        }
    }
}

上面的示例将每 3 秒更改一次 imageView 的可见性,尽管从您的示例中不清楚这是否是您的意图。

【讨论】:

    猜你喜欢
    • 2012-07-22
    • 2014-04-04
    • 2019-12-09
    • 1970-01-01
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    相关资源
    最近更新 更多