Android 模拟器显示灰屏
Android emulator shows gray screen
我正在尝试创建一个函数,使 api 每 30 秒调用一次服务器并每次使用新数据更新 UI。我做了一个无限循环,在其中迭代具有固定数量端点的数组,并在这个循环中调用我的函数。但是当我 运行 模拟器上的应用程序时,应用程序什么也不显示,只有深灰色背景:
它也没有记录任何错误,所以我不知道可能是什么问题。下面是无限循环的代码:
private String [] endpoints = new String[] {"https://server.com/api/update-rates?locale=en",
"https://server.com/api/update-rates?locale=fr",
"https://server.com/api/update-rates?locale=gr"};
Log.d("forloop", "test1");
while(true) {
Log.d("forloop", "test2");
for (int i = 0; i < endpoints.length; i++) {
getFreshRates(endpoints[i]);
Log.d("forloop", String.valueOf(i));
try {
TimeUnit.SECONDS.sleep(30);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
if(i == 2) {
i = -1;
}
}
}
我保证 getFreshRates
方法正常工作(我已经用静态 url 测试过)所以问题可能出在这个循环或某些 android 特定问题上。
Log.d 输出是:
2022-03-04 10:56:15.741 8359-8359/com.example.currencyrates D/forloop: test1
2022-03-04 10:56:15.741 8359-8359/com.example.currencyrates D/forloop: test2
2022-03-04 10:56:16.270 8359-8359/com.example.currencyrates D/forloop: 0
2022-03-04 10:56:46.279 8359-8359/com.example.currencyrates D/forloop: 1
2022-03-04 10:57:16.281 8359-8359/com.example.currencyrates D/forloop: 2
2022-03-04 10:57:46.288 8359-8359/com.example.currencyrates D/forloop: 0
2022-03-04 10:58:16.295 8359-8359/com.example.currencyrates D/forloop: 1
2022-03-04 10:58:46.304 8359-8359/com.example.currencyrates D/forloop: 2
2022-03-04 10:59:16.310 8359-8359/com.example.currencyrates D/forloop: 0
2022-03-04 10:59:46.317 8359-8359/com.example.currencyrates D/forloop: 1
2022-03-04 11:00:16.320 8359-8359/com.example.currencyrates D/forloop: 2
2022-03-04 11:00:46.332 8359-8359/com.example.currencyrates D/forloop: 0
2022-03-04 11:01:16.341 8359-8359/com.example.currencyrates D/forloop: 1
.....
.....
.....
// and so on, so the for loop works as expected too
正如我所说,一旦我删除了这个 while loop
并使用 static url
调用了一个函数,一切都很完美,但问题是我需要每 30 秒使用不同的端点无限调用一次。可能是什么问题?
我已经使用 Handlers
解决了这个问题
// Create the Handler
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
// Start the Runnable immediately
handler.post(runnable);
}
static int i = 0;
// Define the code block to be executed
private Runnable runnable = new Runnable() {
@Override
public void run() {
Log.d("statici", String.valueOf(i));
getFreshRates(endpoints[i]);
i++;
if(i == 3) {
i = 0;
}
// Repeat every 30 seconds
handler.postDelayed(runnable, 30000);
}
};
我正在尝试创建一个函数,使 api 每 30 秒调用一次服务器并每次使用新数据更新 UI。我做了一个无限循环,在其中迭代具有固定数量端点的数组,并在这个循环中调用我的函数。但是当我 运行 模拟器上的应用程序时,应用程序什么也不显示,只有深灰色背景:
它也没有记录任何错误,所以我不知道可能是什么问题。下面是无限循环的代码:
private String [] endpoints = new String[] {"https://server.com/api/update-rates?locale=en",
"https://server.com/api/update-rates?locale=fr",
"https://server.com/api/update-rates?locale=gr"};
Log.d("forloop", "test1");
while(true) {
Log.d("forloop", "test2");
for (int i = 0; i < endpoints.length; i++) {
getFreshRates(endpoints[i]);
Log.d("forloop", String.valueOf(i));
try {
TimeUnit.SECONDS.sleep(30);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
if(i == 2) {
i = -1;
}
}
}
我保证 getFreshRates
方法正常工作(我已经用静态 url 测试过)所以问题可能出在这个循环或某些 android 特定问题上。
Log.d 输出是:
2022-03-04 10:56:15.741 8359-8359/com.example.currencyrates D/forloop: test1
2022-03-04 10:56:15.741 8359-8359/com.example.currencyrates D/forloop: test2
2022-03-04 10:56:16.270 8359-8359/com.example.currencyrates D/forloop: 0
2022-03-04 10:56:46.279 8359-8359/com.example.currencyrates D/forloop: 1
2022-03-04 10:57:16.281 8359-8359/com.example.currencyrates D/forloop: 2
2022-03-04 10:57:46.288 8359-8359/com.example.currencyrates D/forloop: 0
2022-03-04 10:58:16.295 8359-8359/com.example.currencyrates D/forloop: 1
2022-03-04 10:58:46.304 8359-8359/com.example.currencyrates D/forloop: 2
2022-03-04 10:59:16.310 8359-8359/com.example.currencyrates D/forloop: 0
2022-03-04 10:59:46.317 8359-8359/com.example.currencyrates D/forloop: 1
2022-03-04 11:00:16.320 8359-8359/com.example.currencyrates D/forloop: 2
2022-03-04 11:00:46.332 8359-8359/com.example.currencyrates D/forloop: 0
2022-03-04 11:01:16.341 8359-8359/com.example.currencyrates D/forloop: 1
.....
.....
.....
// and so on, so the for loop works as expected too
正如我所说,一旦我删除了这个 while loop
并使用 static url
调用了一个函数,一切都很完美,但问题是我需要每 30 秒使用不同的端点无限调用一次。可能是什么问题?
我已经使用 Handlers
解决了这个问题// Create the Handler
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
// Start the Runnable immediately
handler.post(runnable);
}
static int i = 0;
// Define the code block to be executed
private Runnable runnable = new Runnable() {
@Override
public void run() {
Log.d("statici", String.valueOf(i));
getFreshRates(endpoints[i]);
i++;
if(i == 3) {
i = 0;
}
// Repeat every 30 seconds
handler.postDelayed(runnable, 30000);
}
};