我正在试图让一个图像每半小时更换一次的Android工作室项目,我遵循一个示例但没有运气;它不引发错误,它只是doesen't工作,这里就是我有 什么,我已经试过:在特定时间更改图像
public class MainActivity extends AppCompatActivity {
long startTime = 0;
Handler feed = new Handler();
Runnable runTimer = new Runnable()
{
@Override
public void run() {
Calendar now = Calendar.getInstance();
TextView output = (TextView) findViewById(R.id.output);
ImageView roto = (ImageView) findViewById(R.id.earthView);
Globals g = (Globals) getApplication();
int vision = 0;
int visionb = 0;
int hour = now.get(Calendar.HOUR);
int minute = now.get(Calendar.MINUTE);
String[] epos = g.getData56();
if (minute >= 30) {
visionb = 1;
}
if (minute < 30) {
visionb = 0;
}
long millis = System.currentTimeMillis() - startTime;
int seconds = (int) (millis/1000);
int minutes = seconds/60;
seconds = seconds % 60;
output.setText(String.format("%d:%02d", minutes, seconds));
vision = ((hour * 2) + visionb);
int resID = getResources().getIdentifier(epos[vision], "drawable", getPackageName());
if (roto != null) {
roto.setImageResource(resID);
feed.postDelayed(this, 500); // tried moving this around and changing the value from 0 - 3000
}
startTime = System.currentTimeMillis();
feed.postDelayed(runTimer, 500); // tried moving this around and changing the value from 0 - 3000
}
};
为了测试我的代码,我把这个在onUserInteraction方法和它的作品完美的罚款:
@Override
public void onUserInteraction() {
super.onUserInteraction();
final Globals g = (Globals) getApplication();
Calendar now = Calendar.getInstance();
ImageView roto = (ImageView) findViewById(R.id.earthView);
int vision = 0;
int visionb = 0;
int hour = now.get(Calendar.HOUR);
int minute = now.get(Calendar.MINUTE);
String[] epos = g.getData56();
if (minute >= 30) {
visionb = 1;
}
if (minute < 30) {
visionb = 0;
}
vision = ((hour * 2) + visionb);
int resID = getResources().getIdentifier(epos[vision], "drawable", getPackageName());
if (roto != null) {
roto.setImageResource(resID);
}
}
所以我假设我做错了什么,任何人都知道我做错了或失踪?
感谢您的帮助Landice这是工作成果,给我一个时钟和每一个小时 改变我的形象(其中有48个全部图像),再次感谢您的帮助!
long startTime = 0;
{
final Handler feed = new Handler();
final Runnable runTimer = new Runnable() {
@Override
public void run() {
Calendar now = Calendar.getInstance();
TextView output = (TextView) findViewById(R.id.output);
ImageView roto = (ImageView) findViewById(R.id.earthView);
Globals g = (Globals) getApplication();
int vision = 0;
int visionb = 0;
int second = now.get(Calendar.SECOND);
int hour = now.get(Calendar.HOUR);
int minute = now.get(Calendar.MINUTE);
String[] epos = g.getData56();
if (minute >= 30) {visionb = 1;}
if (minute < 30) {visionb = 0;}
output.setText(String.format(hour + ":" + minute + ":" + second));
feed.postDelayed(this, 0);
if (minute == 30 || minute == 0) {vision = ((hour * 2) + visionb);
int resID = getResources().getIdentifier(epos[vision], "drawable", getPackageName());
if (roto != null) {
roto.setImageResource(resID);
}
}
startTime = System.currentTimeMillis();
}
};
feed.postDelayed(runTimer,1000);
}
谢谢是的,这是我做什么,我没有scedual第一发布并且将handler.post(runTimer)放在错误的地方 – Wraithious