【发布时间】:2013-09-01 21:26:33
【问题描述】:
我迷路了。我需要在应用程序启动时读取目录中的文件并从中制作按钮。我必须使用 while 循环,并且必须更新 UI。我已经尝试了很长一段时间来运行一个可运行文件,并且只有循环内的代码在 UI 线程中运行。我对 android 比较陌生,但这似乎很简单。
这段代码就是我现在所拥有的。它不会抛出任何错误或警告,但它什么也不做。我知道按钮制作代码有效,因为“添加按钮”按钮可以正确制作按钮。我不知道为什么它不起作用。 (这在 OnCreate 中运行)
Runnable aRunnable = new Runnable() {
public void run() {
Looper.prepare();
File f = new File(Environment.getExternalStorageDirectory() + "/myapp/");
File[] filearray = f.listFiles();
int amount = filearray.length;
final String[] files = new String[amount];
int count = 0;
while (count != amount) {
files[count] = filearray[count].toString();
count += 1;
}
int times = files.length;
int counter = 0;
while (counter != times) {
Handler handler = new Handler();
handler.post(new Runnable() {
public void run() {
// Button making code
}
});
}
Looper.loop();
}
};
Thread thread = new Thread(aRunnable);
thread.start();
【问题讨论】:
-
你能定义“它不工作”吗?您说“按钮制作代码”有效,并且您没有错误或警告,所以发生了什么/没有发生什么?
-
应用程序启动并运行良好,但没有生成任何按钮。
标签: android multithreading user-interface