我有一个在后台借助Handler定期运行任务的函数。定期执行的代码的一部分是更改UI,因此我在主线程中运行它。该代码可以运行,但是两行代码(用于更改UI)似乎从未执行过。请查看下面代码中的注释以获取更多详细信息。
public void startTimerForRefreshingSensorsData(Integer mins) {
if (handlerRefresh == null) {
final int delay = mins * 60000;//milliseconds
final TextView refreshes = findViewById(R.id.number_of_autorefreshes);
final TextView refreshes_time = findViewById(R.id.autorefresh_time);
refreshes.setText("Autorefreshes:" + numberOfAutoRefreshes);
final DateFormat df = new SimpleDateFormat("dd MM yyyy, HH:mm");
handlerThread = new HandlerThread("refreshThread");
handlerThread.start();
handlerRefresh = new Handler(handlerThread.getLooper());
//Running the handler
handlerRefresh.postDelayed(new Runnable() {
public void run() {
readJsonFromMultipleSensorsUrlAndStoreAsSharedpreference();
lastAutoRefreshTimeSensorList = df.format(Calendar.getInstance().getTime());
numberOfAutoRefreshes += 1;
// Stuff that updates the UI should run on main thread
runOnUiThread(new Runnable() {
@Override
public void run() {
displaySavedSensorsFromSharedPreferences(); //LOOK LIKE THIS DOES NOT EXECUTE, UI NOT CHANGING
refreshes_time.setText("Last refresh: " + lastAutoRefreshTimeSensorList);
refreshes.setText("Autorefreshes:" + numberOfAutoRefreshes); //THIS DOES NOT EXECUTE, UI NOT CHANGING
Log.d("REFRESH HANDLER", "Some stuff ran on the UI thread"); //THIS LINE EXECUTES, I get a message in the log ???
}
});
Log.d("REFRESH HANDLER", "The " + numberOfAutoRefreshes + " handler has been run at: " + df.format(Calendar.getInstance().getTime()));
handlerRefresh.postDelayed(this, delay);
}
}, delay);
}
}
在活动的onCreate函数中调用此方法。 LMK如果需要任何其他信息...谢谢!
我相信该函数displaySavedSensorsFromSharedPreferences();导致一些我不知道的问题,这是它的代码:
//Generates the arrays with values for the CustomAdaprter class, if the latter breaks, the problem is most probably here
Log.d("JSONException)", "************************ START ALL *********************************");
ListView sensorsListView = findViewById(R.id.sensorsView);
//Get the saved sensors list //TODO - what happens when NULL ???
ArrayList<String> list = getSavedSensorsList();
Log.d("JSONException)", "Getting the saved sensors list "+list);
//Set up all arrays with data for the sensors list
int lSize = list.size();
Log.d("JSONException)", "The size of the list is "+lSize);
String[] idsArr = new String[lSize];
Log.d("JSONException)", "String array -isdArr- of size "+idsArr.length+" generated");
idsArr = list.toArray(idsArr);
Log.d("JSONException)", "String array populated with list values");
Integer[] iconIdsArr = new Integer[lSize]; //TODO - add logic for generating this Integer[] array and remove it from the dummy generation in the FOR loop below
Log.d("JSONException)", "String array -iconIdsArr- of size "+iconIdsArr.length+" generated");
Log.d("JSONException)", "Generating values arrays:");
String[] valuesArr = new String[lSize];
Log.d("JSONException)", "ValuesArr- size "+valuesArr.length);
String[] valueTypesArr = new String[lSize];
Log.d("JSONException)", "valueTypesArr- size "+valueTypesArr.length);
String[] valuesArr2 = new String[lSize];
Log.d("JSONException)", "valuesArr2- size "+valuesArr2.length);
String[] valueTypesArr2 = new String[lSize];
Log.d("JSONException)", "valueTypesArr2- size "+valueTypesArr2.length);
//Populate the set arrays
Log.d("JSONException)", "Populating arrays");
for (int i=0; i<lSize;i++) {
Log.d("JSONException)", ".............START SENSOR "+list.get(i)+"................");
String sensorJsonString = readAStringPreference(list.get(i));
Log.d("JSONException)", "Starting extracting the JSON string for sensor "+list.get(i));
Log.d("JSONException)", "JSON string: "+sensorJsonString);
if (!sensorJsonString.isEmpty()) {
Log.d("JSONException)", "Passed IF, the JSON string is not NULL");
try {
JSONArray jar2 = new JSONArray(sensorJsonString);
Log.d("JSONException)", "Creating the JSON array from the string for sensor "+list.get(i));
valuesArr[i] = jar2.getJSONObject(0).getString("value");
valueTypesArr[i] = jar2.getJSONObject(0).getString("value_type");
Log.d("JSONException)", valuesArr[i] +" "+ valueTypesArr[i]);
valuesArr2[i] = jar2.getJSONObject(1).getString("value");
valueTypesArr2[i] = jar2.getJSONObject(1).getString("value_type");
Log.d("JSONException)", valuesArr2[i] +" "+ valueTypesArr2[i]);
iconIdsArr[i] = 1; //TODO - check the upper TODO
} catch (JSONException e) {
e.printStackTrace();
displayMessageIfDebuggingOn("There is a JSON exception, check the logs...");
Log.d("JSONException)", "JSON throwing some exception...");
}
} else {
Log.d("JSONException", "JSON for sensor "+list.get(i)+" is EMPTY, ENDING CYCLE!!!");
}
Log.d("JSONException)", ".............END SENSOR "+list.get(i)+"................");
Log.d("JSONException)", "");
}
Log.d("JSONException)", "*************************END ALL***************************************");
Log.d("JSONException)", "");
//Apply custom adapter to populated data
CustomAdapter adapter = new CustomAdapter(this, idsArr, iconIdsArr, valuesArr, valueTypesArr, valuesArr2, valueTypesArr2);
sensorsListView.setAdapter(adapter);
View headerView = ((LayoutInflater)this.getSystemService(this.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.sensors_list_header, null, false);
if (sensorsListView.getHeaderViewsCount() < 1) {
sensorsListView.addHeaderView(headerView);
}
}```