其他分享
首页 > 其他分享> > 引起:android.os.NetworkOnMainThreadException

引起:android.os.NetworkOnMainThreadException

作者:互联网

嗨所有的观众和读者,我使用link.每当我在单独的包中运行此代码它工作正常,但每当我试图在我的应用程序中嵌入此示例时,得到遗憾的应用程序已经停止,只有当我点击特定的选项卡调用它的标签,Logcat说: –

Caused by: android.os.NetworkOnMainThreadException
10-30 15:07:35.341: E/AndroidRuntime(732):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117),

请告诉我我需要做什么使它对我有用,除了这个特定的选项卡整个应用程序工作正常.

  10-30 15:07:26.770: D/dalvikvm(732): GC_FOR_ALLOC freed 61K, 3% free 8035K/8259K, paused 39ms, total 46ms
    10-30 15:07:26.781: I/dalvikvm-heap(732): Grow heap (frag case) to 8.481MB for 614416-byte allocation
    10-30 15:07:26.850: D/dalvikvm(732): GC_CONCURRENT freed 1K, 4% free 8634K/8903K, paused 27ms+13ms, total 71ms
    10-30 15:07:26.881: D/dalvikvm(732): GC_FOR_ALLOC freed 0K, 4% free 8634K/8903K, paused 28ms, total 28ms
    10-30 15:07:26.900: I/dalvikvm-heap(732): Grow heap (frag case) to 9.798MB for 1382416-byte allocation
    10-30 15:07:26.970: D/dalvikvm(732): GC_CONCURRENT freed 0K, 4% free 9984K/10311K, paused 28ms+5ms, total 72ms
    10-30 15:07:27.110: D/gralloc_goldfish(732): Emulator without GPU emulation detected.
    10-30 15:07:30.591: D/dalvikvm(732): GC_FOR_ALLOC freed 675K, 8% free 9875K/10695K, paused 33ms, total 62ms
    10-30 15:07:30.671: D/dalvikvm(732): GC_FOR_ALLOC freed 519K, 9% free 9806K/10695K, paused 30ms, total 31ms
    10-30 15:07:30.740: D/dalvikvm(732): GC_FOR_ALLOC freed 310K, 10% free 9713K/10695K, paused 28ms, total 29ms
    10-30 15:07:30.751: I/dalvikvm-heap(732): Grow heap (frag case) to 10.488MB for 1000016-byte allocation
    10-30 15:07:30.820: D/dalvikvm(732): GC_CONCURRENT freed <1K, 9% free 10690K/11719K, paused 25ms+5ms, total 74ms
    10-30 15:07:30.911: D/dalvikvm(732): GC_FOR_ALLOC freed 0K, 9% free 10690K/11719K, paused 36ms, total 36ms
    10-30 15:07:30.931: I/dalvikvm-heap(732): Grow heap (frag case) to 12.633MB for 2250016-byte allocation
    10-30 15:07:31.030: D/dalvikvm(732): GC_CONCURRENT freed 0K, 8% free 12887K/13959K, paused 32ms+16ms, total 93ms
    10-30 15:07:31.211: D/dalvikvm(732): GC_FOR_ALLOC freed 3683K, 30% free 10314K/14599K, paused 35ms, total 38ms
    10-30 15:07:31.211: I/dalvikvm-heap(732): Grow heap (frag case) to 11.439MB for 1382416-byte allocation
    10-30 15:07:31.360: D/dalvikvm(732): GC_CONCURRENT freed <1K, 21% free 11663K/14599K, paused 81ms+5ms, total 144ms
    10-30 15:07:31.551: D/dalvikvm(732): GC_FOR_ALLOC freed 2555K, 32% free 9936K/14599K, paused 32ms, total 38ms
    10-30 15:07:31.561: I/dalvikvm-heap(732): Grow heap (frag case) to 10.706MB for 1000016-byte allocation
    10-30 15:07:31.701: D/dalvikvm(732): GC_CONCURRENT freed 1K, 26% free 10912K/14599K, paused 78ms+16ms, total 139ms
    10-30 15:07:31.770: D/dalvikvm(732): GC_FOR_ALLOC freed 0K, 26% free 10912K/14599K, paused 31ms, total 31ms
    10-30 15:07:31.770: I/dalvikvm-heap(732): Grow heap (frag case) to 12.850MB for 2250016-byte allocation
    10-30 15:07:31.871: D/dalvikvm(732): GC_CONCURRENT freed 0K, 11% free 13109K/14599K, paused 29ms+6ms, total 91ms
    10-30 15:07:32.060: D/dalvikvm(732): GC_FOR_ALLOC freed 1134K, 11% free 13102K/14599K, paused 35ms, total 39ms
    10-30 15:07:32.070: I/dalvikvm-heap(732): Grow heap (frag case) to 14.162MB for 1382416-byte allocation
    10-30 15:07:32.150: D/dalvikvm(732): GC_FOR_ALLOC freed <1K, 10% free 14451K/16007K, paused 36ms, total 36ms
    10-30 15:07:32.161: I/Choreographer(732): Skipped 460 frames!  The application may be doing too much work on its main thread.
    10-30 15:07:35.280: D/AndroidRuntime(732): Shutting down VM
    10-30 15:07:35.280: W/dalvikvm(732): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
    10-30 15:07:35.341: E/AndroidRuntime(732): FATAL EXCEPTION: main
    10-30 15:07:35.341: E/AndroidRuntime(732): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.erachnida.restaurant.versionoct/com.erachnida.restaurant.versionoct.CustomizedListView}: android.os.NetworkOnMainThreadException
    10-30 15:07:35.341: E/AndroidRuntime(732):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at android.app.ActivityThread.startActivityNow(ActivityThread.java:1900)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:694)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at android.widget.TabHost.setCurrentTab(TabHost.java:358)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:150)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at android.view.View.performClick(View.java:4084)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at android.view.View$PerformClick.run(View.java:16966)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at android.os.Handler.handleCallback(Handler.java:615)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at android.os.Handler.dispatchMessage(Handler.java:92)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at android.os.Looper.loop(Looper.java:137)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at android.app.ActivityThread.main(ActivityThread.java:4745)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at java.lang.reflect.Method.invokeNative(Native Method)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at java.lang.reflect.Method.invoke(Method.java:511)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at dalvik.system.NativeStart.main(Native Method)
    10-30 15:07:35.341: E/AndroidRuntime(732): Caused by: android.os.NetworkOnMainThreadException
    10-30 15:07:35.341: E/AndroidRuntime(732):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at java.net.InetAddress.getAllByName(InetAddress.java:214)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at com.erachnida.restaurant.versionoct.XMLParser.getXmlFromUrl(XMLParser.java:45)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at com.erachnida.restaurant.versionoct.CustomizedListView.onCreate(CustomizedListView.java:42)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at android.app.Activity.performCreate(Activity.java:5008)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
    10-30 15:07:35.341: E/AndroidRuntime(732):  ... 18 more
    10-30 15:07:42.250: I/Process(732): Sending signal. PID: 732 SIG: 9

解决方法:

这个问题有两个解决方案.

1)不要在主UI线程中编写网络调用,为此使用异步任务.

2)在setContentView(R.layout.activity_main)之后将下面的代码写入MainActivity文件;

if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

并将import语句放到你的java文件中.

import android.os.StrictMode;

标签:android,android-emulator,networkonmainthread,android-parser
来源: https://codeday.me/bug/20190917/1809907.html