我有一个问题得到一个简单的循环,在Java中运行就好,在Android中工作。简单的循环工作在Java中,但不是“Android”
我创建了一个textview并将其设置为仅用于测试运行。当我运行这个时,我得到一个数字出现在框中,就是这样,它不会改变为另一个数字。
我试过这个for循环,但所有我得到的是标题栏然后黑屏然后崩溃。
在Eclipse中没有错误,她运行并没有错误,只是没有数字更新。
如果你想知道第二个while循环它有点像暂停,所以你可以看到数字显示较慢,并在纯Java中工作。
Just and FYI我一直在从YouTube上观看vids到学习Java两周。我已经创建了几个Android应用程序,这些应用程序能够完成整理,但无法在Android中获得循环。
感谢任何帮助新人。
public class MainActivity extends ActionBarActivity {
TextView display;
int counter;
int min = 1;
int max = 5;
int counter2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = (TextView) findViewById(R.id.textView1);
while(counter < 10){
Random r = new Random();
int rn = r.nextInt(max - min + 1) + min;
String rando = Integer.toString(rn);
display.setText(rando);
while(counter2 < 5000){
counter2++;
}
counter++;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
您从未实例化过counter或counter2。你必须先将它们设置为0 ... – aProperFox 2014-10-17 18:14:37
对不起,我知道设置这些,并在一个点做了,但一个视频显示他们不设置为0.我通过添加= 0将它们设置为0;到整数。仍然没有去。 – reddragon72 2014-10-17 18:18:28
啊我更好地阅读你的文章。你为什么期望它显示数以千计的数字?确定它效率低下,需要一段时间,但它以与计算它们相同的速率显示数字,这是非常快的。如果你想看它随着时间的推移而增加,请在那里添加一个Thread.sleep(100)呼叫 – aProperFox 2014-10-17 18:37:21