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 的教程之一,我设法创建了上述代码,该代码用于创建 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

您的timer变量是您的onCreate()方法的本地变量,但您正尝试以synchronized不同的方法(通过)访问它 - 所以它没有解决。您需要转为成为类数据成员,或者使用其范围在您的方法timer中可用的其他对象。onTouchEvent()

于 2012-12-08T13:11:10.350 回答
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 回答
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 回答