3

在我的 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)。并返回上一个显示剩余 Imageviews 的活动,如上面的代码:-)

4

1 回答 1

3

在 5000 毫秒后使用这个处理程序做任何事情

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

                          }
                      }, 5000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
于 2013-02-27T07:05:01.870 回答