Closed travy closed 7 years ago
This issue was being caused by modifying the visibility of error labels on the MainActivity
thread while still in the SettingsActivity
thread.
Essentially, while in the SettingsActivity
's thread, whenever the user entered in an invalid api key the UnauthorizedNetworkException would be thrown within the Networking thread as you could see from this code snippet.
// This is the codeblock in `MovieDbRequester.java` that performs the network request
try {
...
if (json != null) {
movieList = MovieDbParser.retrieveMovieList(json, parentActivity);
...
}
}
...
} catch (HttpUnauthorizedException e) {
e.printStackTrace();
errorHandler.onUnauthorizedAccess(); // This code updates a View on MainActivity from SettingsActivity
} catch (NetworkingException e) {
...
}
After the exception is caught, the app then performs the action errorHandler.onUnauthorizedAccess()
which is a method on the MainActivity
which will change the visibility of a View
label.
// method from MainActivity
@Override
public void onUnauthorizedAccess() {
mUnauthorizedTextView.setVisibility(View.VISIBLE);
}
This causes the app to crash since the View
field being modified exists on the thread for MainActivity
but is being modified from the thread created for the SettingsActivity
.
The issue could be resolved by forcing the view element to be modified on the correct thread. The following code block should remedy the issue.
// forces the view to be modified on the correct thread
@Override
public void onUnauthorizedAccess() {
runOnUiThread(new Runnable() {
@Override
public void run() {
mUnauthorizedTextView.setVisibility(View.VISIBLE);
}
});
}
The same should be done for any other View
's that are being modified within the SettingsActivity
thread.
The bug has been fixed.
The app keeps crashing after specifying an invalid api key.
Log Exception