Java 如何在onfailure方法中祝酒
因此,我在一个单独的服务中使用OkHttp从twitter API获取数据。 我一直在试图提醒用户,没有互联网连接就无法加载推文。但当我尝试此操作时,应用程序崩溃。错误是:java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序。这是密码Java 如何在onfailure方法中祝酒,java,android,android-studio,twitter,okhttp,Java,Android,Android Studio,Twitter,Okhttp,因此,我在一个单独的服务中使用OkHttp从twitter API获取数据。 我一直在试图提醒用户,没有互联网连接就无法加载推文。但当我尝试此操作时,应用程序崩溃。错误是:java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序。这是密码 private void getTweets(final String topic) { final TwitterService twitterService = new Twitter
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();
}
可能您从错误的线程调用Toast.makeText。它需要从UI线程调用。试试这个
activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show();
}
});
这里有更多信息您正在从工作线程调用它。您需要从主线程内调用Toast.makeText()(以及处理UI的大多数其他函数)。例如,您可以使用处理程序 //在UI线程中设置此项
mHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message message) {
// This is where you do your work in the UI thread.
// Your worker tells you in the message what to do.
}
})
}
void workerThread() {
// And this is how you call it from the worker thread:
Message message = mHandler.obtainMessage(command, parameter);
message.sendToTarget();