I use OkHttp in a separate service to fetch data from the twitter API.
I have been trying to Alert the user that tweets cannot be loaded without an internet connection. But when i try this, the application crashes.The error is
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
.
Here’s my code
private void getTweets(final String topic) {
final TwitterService twitterService = new TwitterService();
twitterService.findTweets(topic, new Callback() {
@Override
public void onFailure(Request request, IOException e) {
e.printStackTrace();
Log.e("Traffic Activity", "Failed to make API call");
Toast.makeText(getApplicationContext(), "Bigger fail", Toast.LENGTH_LONG).show();
}
2
Answers
Probably you are calling Toast.makeText from the wrong thread. It needs to be called from the UI thread. Try this instead
More informations here Can't create handler inside thread that has not called Looper.prepare()
You’re calling it from a worker thread. You need to call Toast.makeText() (and most other functions dealing with the UI) from within the main thread. You could use a handler, for example
//set this up in UI thread
};
}