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 に答える