Android + FATAL EXCEPTION:Timer-0(使用计时器删除ImageView)

时间:2013-02-26 13:30:33

标签: android timer

在我的Android应用程序中,我使用计时器来显示使用TIMER的相应ImageView。 我收到了一个错误

02-27 06:39:33.999: E/AndroidRuntime(1325): FATAL EXCEPTION: Timer-0
02-27 06:39:33.999: E/AndroidRuntime(1325):       android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that  created a view hierarchy can touch its views.
02-27 06:39:33.999: E/AndroidRuntime(1325):     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746)
02-27 06:39:33.999: E/AndroidRuntime(1325):     at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:823)
02-27 06:39:33.999: E/AndroidRuntime(1325):     at android.view.View.requestLayout(View.java:15468)
02-27 06:39:33.999: E/AndroidRuntime(1325):     at android.view.View.requestLayout(View.java:15468) 
02-27 06:39:33.999: E/AndroidRuntime(1325):     at android.view.View.requestLayout(View.java:15468)
02-27 06:39:33.999: E/AndroidRuntime(1325):     at android.view.View.requestLayout(View.java:15468)
02-27 06:39:33.999: E/AndroidRuntime(1325):     at android.view.View.requestLayout(View.java:15468)
02-27 06:39:33.999: E/AndroidRuntime(1325):     at android.view.View.setFlags(View.java:8437)
02-27 06:39:33.999: E/AndroidRuntime(1325):     at android.view.View.setVisibility(View.java:5714)
02-27 06:39:33.999: E/AndroidRuntime(1325):     at android.widget.ImageView.setVisibility(ImageView.java:1161)
02-27 06:39:33.999: E/AndroidRuntime(1325):     at com.SAI.timer.MainActivity$1.run(MainActivity.java:51)
02-27 06:39:33.999: E/AndroidRuntime(1325):     at java.util.Timer$TimerImpl.run(Timer.java:284)

代码是:(如何使图像视图消失在这里,)

public class MainActivity extends Activity {

MediaPlayer mp;
ImageView firstImageView,secondImageView,thirdImageView,fourthImageView,fullImageView;
Timer t;
TimerTask welcome;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    firstImageView = (ImageView)findViewById(R.id.imageView1);
    secondImageView = (ImageView)findViewById(R.id.imageView2);
    thirdImageView = (ImageView)findViewById(R.id.imageView3);
    fourthImageView = (ImageView)findViewById(R.id.imageView4);
    fullImageView = (ImageView)findViewById(R.id.imageView5);


      firstImageView.setOnClickListener (new ClickListener());
      secondImageView.setOnClickListener(new ClickListener());
      thirdImageView.setOnClickListener (new ClickListener());
      fourthImageView.setOnClickListener(new ClickListener());


      mp = MediaPlayer.create(getApplicationContext(), R.raw.yes);

      t = new Timer();
      welcome = new TimerTask(){

            @Override
            public void run() {
                // TODO Auto-generated method stub
                fullImageView.setVisibility(View.GONE);

            }
        };


}


class ClickListener implements OnClickListener {
    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()) {
        case R.id.imageView1:

            mp.start();



                firstImageView.setVisibility(View.GONE);
                secondImageView.setVisibility(View.GONE);
                thirdImageView.setVisibility(View.GONE);
                fourthImageView.setVisibility(View.GONE);

                fullImageView.setVisibility(View.VISIBLE);
                fullImageView.setImageResource(R.drawable.ic_launcher);

                t.schedule(welcome, 5000);



            break;

        case R.id.imageView2:


            Toast.makeText(getApplicationContext(), "Oh !!! Wrong Yaar", Toast.LENGTH_SHORT).show();



            break;

这里我想在媒体播放(5秒)停止后让“fullImageView”消失(View.GONE)。 并返回上一个显示剩余Imageview的活动,如上面的代码所示: - )

1 个答案:

答案 0 :(得分:3)

使用此处理程序在5000毫秒后执行任何操作

try {
                    new Handler().postDelayed(new Runnable() {
                          @Override
                          public void run() {
                     //Your process to do

                          }
                      }, 5000);
                } catch (Exception e) {
                    e.printStackTrace();
                }