Error reason:
You can't refresh the UI thread in a subthread in Android
resolvent:
//Refresh the UI using the runOnUiThread method
runOnUiThread(new Runnable() {
@Override
public void run() {
//refresh UI
}
});
common problem:
When calling locationmanager to get the current location information and upload it in real time, the reason for this error is that locationmanager needs to be used in the main thread. The following methods are recommended:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//upload location messages
Button btnUploadGPS = (Button)findViewById(R.id.btn_upload_gps);
if (btnUploadGPS != null){
btnUploadGPS .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadGPS();
}
});
}
}
private void uploadGPS(){
final LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
2000,
(float) 0.01,
new LocationListener() {
@Override
public void onLocationChanged(Location location) {
asyncUploadGPS(location);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
asyncUploadGPS(locationManager.getLastKnownLocation(provider));
}
@Override
public void onProviderDisabled(String provider) {
asyncUploadGPS(null);
}
}
);
}
private void asyncUploadGPS(final Location currentLocation) {
new Thread(new Runnable() {
@Override
public void run() {
//upload loacation messages
}
}).start();
}
Read More:
- The showdialog() method in thread/threading. Timer/task reported an error: “before ole can be called, the current thread must be set to single thread unit (STA) mode.”
- Android integration of iFLYTEK’s speech recognition (voice dictation) error message “Failed to create the object, please confirm that libmsc.so is placed correctly, and createUtility is called to initialize”
- [Solved] Android mediaplayer.prepare() Error: Caused by: java.lang.IllegalStateException
- ROS package executes rosrun error: attributeerror: ‘thread’ object has no attribute ‘isalive‘
- Conda create New environment Error: An unexpected error has occurred. Conda has prepared the above report.
- error: rpmdb: BDB0113 Thread/process 14536/140712790841152 failed: BDB1507 Thread died in Berkeley DB library
- [Solved] Android Studio Error:This view is not constrained, it only has designtime positions
- How to Solve Android Error: gps requires ACCESS_FINE_LOCATION
- [Solved] ECharts Console Error: `resize` should not be called during main process
- [Solved] Redisson distributed lock error: attempt to unlock lock, not locked by current thread by node id
- No repositories directory found inside registry_ DATA_ DIR
- Google Play googleapi: Error 403: Google Play Android Developer API has not been used in project
- How to Solve Error: Android java.lang.IllegalStateException: Could not execute method of the activity
- [Solved] Android Develop Error: xxx does not have a NavController set on xxx
- [Solved] Xcode error: cycle inside*******; building could produce unreliable results.
- JZVideo Error: Attempt to invoke virtual method ‘android.view.Window android.app.Activity.getWindow()’ on a null object reference
- Android 10 open file exception open failed: eacces (permission denied) android:requestLegacyExternalStorage= “true“
- Flash back problem when Android studio plays music in sdcard through intent
- ElasticSearch Create Index Error: mapper_parsing_exception Root mapping definition has unsupported parameters
- [Solved] the SDK location is inside Studio‘s install location