Issue
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();
}
Solution
Probably you are calling Toast.makeText from the wrong thread. It needs to be called from the UI thread. Try this instead
activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show();
}
});
More informations here Can't create handler inside thread that has not called Looper.prepare()
Answered By - RaffoSorr
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.