无法捕获NetworkOnMainThreadException [英] Can't Catch NetworkOnMainThreadException

查看:123
本文介绍了无法捕获NetworkOnMainThreadException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道什么导致NetworkOnMainThreadException,以及如何解决它,但为了改善开发体验,我想能够捕获异常,至少记录事件或提醒用户(在这一点上谁仍然是开发人员)...



奇怪的是,我没有运气这个代码(通过我的TCP套接字发送和接收):

  try 
{
toServer.println(msg.trim());
resp = fromServer.readLine();
}
catch(android.os.NetworkOnMainThreadException nex)
{...在这里做某事...

Eclipse根本无法识别该异常,我从Android开发者网站复制异常类型 - 我很确定拼写正确..



有什么我不知道Java(也许),这使得这个异常不可取代?



谢谢,
R。

解决方案

我刚刚在我的eclipse版本上测试了这个,它的工作原理很好..我想我会检查你使用的api版本看起来要抛出这个异常,你需要一个最小的api版本11.否则eclipse是怪?我所知道的是这段代码是正确的,应该执行没有任何问题。


I know what causes a NetworkOnMainThreadException, as well as how to fix it, but for the purposes of improving the development experience, I'd like to be able to catch the exception and at least log the event or alert the user (who is still the developer at this point)...

Strangely, I'm not having any luck with this code (which sends and receives over my TCP socket):

try
{
    toServer.println (msg.trim());
    resp = fromServer.readLine();
}
catch (android.os.NetworkOnMainThreadException nex)
{ ... do something here ... }

Eclipse doesn't recognize that exception at all, and I copy-pasta-ed the exception type from the Android Developer website -- I'm pretty sure I spelled it right...

Is there something I don't know about Java (perhaps) that makes this exception uncatchable??

Thanks, R.

解决方案

Well I just tested this on my version of eclipse, and it works just fine.. I guess I would check which version of the api you are using? looks like to throw that exception you need a minimum api version 11. Otherwise perhaps eclipse is to blame? All I know is that this code is correct and should be executing without any issues.

这篇关于无法捕获NetworkOnMainThreadException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆