Android在用运行app的时候直接崩掉,出现这些错误 5C

12-30 02:44:28.058: E/AndroidRuntime(2071): FATAL EXCEPTION: main
12-30 02:44:28.058: E/AndroidRuntime(2071): java.lang.RuntimeException: Unable to create application com.way.chat.activity.MyApplication: android.os.NetworkOnMainThreadException
12-30 02:44:28.058: E/AndroidRuntime(2071): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4394)
12-30 02:44:28.058: E/AndroidRuntime(2071): at android.app.ActivityThread.access$1300(ActivityThread.java:141)
12-30 02:44:28.058: E/AndroidRuntime(2071): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1294)
12-30 02:44:28.058: E/AndroidRuntime(2071): at android.os.Handler.dispatchMessage(Handler.java:99)
12-30 02:44:28.058: E/AndroidRuntime(2071): at android.os.Looper.loop(Looper.java:137)
12-30 02:44:28.058: E/AndroidRuntime(2071): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-30 02:44:28.058: E/AndroidRuntime(2071): at java.lang.reflect.Method.invokeNative(Native Method)
12-30 02:44:28.058: E/AndroidRuntime(2071): at java.lang.reflect.Method.invoke(Method.java:511)
12-30 02:44:28.058: E/AndroidRuntime(2071): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-30 02:44:28.058: E/AndroidRuntime(2071): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-30 02:44:28.058: E/AndroidRuntime(2071): at dalvik.system.NativeStart.main(Native Method)
12-30 02:44:28.058: E/AndroidRuntime(2071): Caused by: android.os.NetworkOnMainThreadException
12-30 02:44:28.058: E/AndroidRuntime(2071): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
12-30 02:44:28.058: E/AndroidRuntime(2071): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
12-30 02:44:28.058: E/AndroidRuntime(2071): at libcore.io.IoBridge.connectErrno(IoBridge.java:144)
12-30 02:44:28.058: E/AndroidRuntime(2071): at libcore.io.IoBridge.connect(IoBridge.java:112)
12-30 02:44:28.058: E/AndroidRuntime(2071): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
12-30 02:44:28.058: E/AndroidRuntime(2071): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
12-30 02:44:28.058: E/AndroidRuntime(2071): at java.net.Socket.connect(Socket.java:842)
12-30 02:44:28.058: E/AndroidRuntime(2071): at com.way.client.Client.start(Client.java:28)
12-30 02:44:28.058: E/AndroidRuntime(2071): at com.way.chat.activity.MyApplication.onCreate(MyApplication.java:29)
12-30 02:44:28.058: E/AndroidRuntime(2071): at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1000)
12-30 02:44:28.058: E/AndroidRuntime(2071): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4391)
12-30 02:44:28.058: E/AndroidRuntime(2071): ... 10 more

(需要代码的可以私我)

13个回答

android 为了不影响 主线程绘制UI,不允许耗时操作在主线程中直接进行, 例如网络访问, 网络访问需要开启子线程进行,利用线程间的通讯,把子线程得到的结果发送给主线程

android.os.NetworkOnMainThreadException
从Honeycomb SDK(3.0)开始,google不再允许网络请求(HTTP、Socket)等相关操作直接在Main Thread类中,其实本来就不应该这样做,直接在UI线程进行网络操作,会阻塞UI、用户体验相当bad!即便google不禁止,一般情况下我们也不会这么做吧~
所以,也就是说,在Honeycomb SDK(3.0)以下的版本,你还可以继续在Main Thread里这样做,在3.0以上,就不行了,建议:和network有关比较耗时的操作放到一个子线程里,然后用Handler消息机制与主线程通信。

NetworkOnMainThreadException你把网络请求在主线程进行了

共13条数据 首页 2
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐