0

次のコードがあります。

package com.example.top_tech_deals;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.VideoView;

public class Splash extends Activity{



@Override
protected void onCreate(Bundle TravisLoveBacon) {
    // TODO Auto-generated method stub
    super.onCreate(TravisLoveBacon);
    setContentView(R.layout.splash);




VideoView vv = (VideoView)this.findViewById(R.id.videoView);

String fileName = "android.resource://" + getPackageName() + "/" + R.raw.splashvid2;

vv.setVideoURI(Uri.parse(fileName));

vv.start();


    Thread timer = new Thread(){
        public void run(){
            try{
                sleep(12000);

            } catch(InterruptedException e){
                e.printStackTrace();

            } finally{
                Intent openStartingPoint = new Intent     ("android.intent.action.MENU");
                startActivity(openStartingPoint);

            }
        }

    };
    timer.start();
}


//Function that will handle the touch

@Override

public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {

            synchronized(timer){

                splashTread.notifyAll();

            }

        }

        return true;
    }


}

Bucky のチュートリアルの 1 つを使用して、12 秒間のスプラッシュ スクリーンを作成するために使用される上記のコードを作成することができました。また、ビデオが再生されるように変更しました。私が抱えている主な問題は、オンラインで見つけた OnTouchEvent であるコードの最後のビットにあります。すべきことは、ユーザーが画面をタップするだけでスプラッシュ画面をスキップできるようにすることです。これにより、ユーザーは MENU ファイルに移動する必要があります。

エラーは次の行にあるようです。

synchronized(timer){

「エラータイマーは変数に解決できません」と言っています

なぜこれが起こっているのですか、どうすれば修正できますか? 助けてくれてありがとう。

4

4 に答える 4

2

コードを参照してください:

package com.example.top_tech_deals;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.VideoView;

public class Splash extends Activity{

Thread timer;

@Override
protected void onCreate(Bundle TravisLoveBacon) {
    // TODO Auto-generated method stub
    super.onCreate(TravisLoveBacon);
    setContentView(R.layout.splash);




VideoView vv = (VideoView)this.findViewById(R.id.videoView);

String fileName = "android.resource://" + getPackageName() + "/" + R.raw.splashvid2;

vv.setVideoURI(Uri.parse(fileName));

vv.start();


    timer = new Thread(){
        public void run(){
            try{
                 synchronized (this) {
        wait(12000);
         }

            } catch(InterruptedException e){
                e.printStackTrace();

            } finally{
                Intent openStartingPoint = new Intent     ("android.intent.action.MENU");
                startActivity(openStartingPoint);

            }
        }

    };
    timer.start();
}


//Function that will handle the touch

@Override

public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {

            synchronized(timer){

                timer.notify();

            }

        }

        return true;
    }


}
于 2012-12-08T13:22:01.170 に答える
1

スレッドを使用する代わりにカウントdownTimerを使用し、オーバーライド メソッド onFinish() で

CountDownTimer countDownTimer =  new CountDownTimer(12000, 1000) {

     public void onTick(long millisUntilFinished) {
       //ToDO
     }

     public void onFinish() {
              Intent openStartingPoint = new Intent     ("android.intent.action.MENU");
              startActivity(openStartingPoint);
     }
  }.start();

そして onTouch() で

@Override

public boolean onTouchEvent(MotionEvent event) {

        startActivity(openStartingPoint);
        countDownTimer.cancle(); 
        return true;
    }
于 2012-12-08T13:15:28.647 に答える
1

timer変数はメソッドに対してローカルですが、別のメソッドonCreate()で ( 経由で) アクセスしようとしているためsynchronized、未解決です。クラス データ メンバーに移行するか、メソッドtimerでスコープを使用できる他のオブジェクトを使用する必要がありますonTouchEvent()

于 2012-12-08T13:11:10.350 に答える
0

タイマー変数がクラススコープで宣言されていないようです.onCreate()関数で宣言されているため、他のメソッドが参照によって取得できないのです。これをこのようなクラス変数として宣言しprivate Thread timer = null; 、oncreate() メソッドで初期化することをお勧めします

@Override
    protected void onCreate(Bundle TravisLoveBacon) {
        // TODO Auto-generated method stub
        super.onCreate(TravisLoveBacon);
        setContentView(R.layout.splash);

        VideoView vv = (VideoView)this.findViewById(R.id.videoView);
        String fileName = "android.resource://" + getPackageName() + "/" + R.raw.splashvid2;
        vv.setVideoURI(Uri.parse(fileName));
        vv.start();

        this.timer = new Thread(){
            public void run(){
                try{
                    sleep(12000);
                } catch(InterruptedException e){
                    e.printStackTrace();
                } finally{
                    Intent openStartingPoint = new Intent     ("android.intent.action.MENU");
                    startActivity(openStartingPoint);
                }
            }
        };
        timer.start();
    } 
于 2012-12-08T13:16:52.937 に答える