Android Timer App问题 [英] Android Timer App questions
问题描述
嘿伙计们,我正在通过制作一个简单的应用程序来学习机器人。我想要做的是一个简单的应用程序,每秒钟点亮一个按钮,然后一秒钟灯关闭,如果用户点亮按钮,它点亮它们得到一个点。点亮的按钮取决于随机数生成器。
我正在使用我在ScheduledExecutorService上找到的教程来创建一个计时器,程序输出正确在我添加代码以更新每个tick的标签后,在控制台中但在更新程序后卡住了。下面是代码
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool( 1 );
public void beepForAnHour()
{
final Runnable beeper = new Runnable()
{
< span class =code-keyword> public void run()
{
// int Min = 1;
// int Max = 3;
// randomNum =(int)(Math.random()*(Max - Min));
time = time + 1;
System.out.println(Integer.toString(time)+ test);
// 没有这2行,计时器保持正确增加
TextView Time =(TextView)findViewById(R.id.lblSetTime);
Time.setText(Integer.toString(time));
}
};
final ScheduledFuture beeperHandle = scheduler.scheduleAtFixedRate(beeper, 0 , 1 ,SECONDS);
scheduler.schedule( new Runnable(){ public void run(){beeperHandle.cancel(true);}}, 60 * 60 ,SECONDS);
}
有什么建议可以解决这个问题吗?还有关于再次点亮按钮的建议吗?我打算让一个随机数生成器点亮一个按钮,具体取决于生成的数量。
我想我也必须在取消部分上有if语句计时器,所以当生命= 0时,计时器停止并且游戏结束,尽管可以稍后查看。所以现在我只需要了解我做错了什么
在这两行中放置一个try / catch,你会看到TextView可能没有被初始化你的应用程序的生命周期点。
那是因为函数findViewById(int)出类
活动 [ ^ ]无法返回有效值。
请阅读活动文档。
Hey guys I''m learning android by making an simple app I though up. What I''m trying to make is an simple app that every second lights up a button then a second later the light turns off, if the user hits the button while its lit up they get a point. the button which gets lit up depends on a random number generator.
I''m currently using a tutorial I found on ScheduledExecutorService to create a timer, the program outputs correctly in the console but gets stuck after updating the program once after I added the code to update a label every tick. Heres the code
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
public void beepForAnHour()
{
final Runnable beeper = new Runnable()
{
public void run()
{
// int Min = 1;
// int Max = 3;
// randomNum = (int) (Math.random() * ( Max - Min ));
time=time+1;
System.out.println(Integer.toString(time)+" test");
//without these 2 lines the timer keeps increasing correctly
TextView Time = (TextView)findViewById(R.id.lblSetTime);
Time.setText(Integer.toString(time));
}
};
final ScheduledFuture beeperHandle = scheduler.scheduleAtFixedRate(beeper, 0, 1, SECONDS);
scheduler.schedule(new Runnable() { public void run() { beeperHandle.cancel(true); } }, 60 * 60, SECONDS);
}
Any suggestions why this is a problem? And any advise on lighting the button up and off again? I planned to have a random number generator light up a button depending on the number generated.
I imagine I would also have to have if statement on the cancel part of the timer so when "lives = 0" the timer stops and the game ends although that can be looked at later. So now I just need to understand what I am doing wrong
Put a try/catch around those 2 lines and you will see that TextView is probably not initalized at that point of your app''s life cycle.
That''s because the function findViewById(int) out of the class
Activity[^] can not return a valid value at that point.
Please read in the Documentation of Activity.
这篇关于Android Timer App问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!