我有以下代码:
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){
其中说“错误计时器无法解析为变量”
为什么会发生这种情况,我该如何解决?谢谢你的帮助。
答案 0 :(得分: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;
}
}
答案 1 :(得分:1)
您的timer
变量是onCreate()
方法的本地变量,但您尝试使用其他方法访问它(通过synchronized
) - 因此它尚未解析。您需要将timer
移动为类数据成员,或者使用其范围在onTouchEvent()
方法中可用的其他对象。
答案 2 :(得分: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;
}
答案 3 :(得分:0)
看来你的timer变量没有在类范围声明,它在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();
}