Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Is it possible to get "android.os.NetworkOnMainThreadException" in Titanium Studio ?

$
0
0

I read some Q&A here and they said Titanium does the UI operations and net work operations in two different threads. My app uses xhr calls for network operations. But my app crashes because of "android.os.NetworkOnMainThreadException". Is this possible? if so how to fix it. listed below the way i'm doing my network calls, BuildLog output. I'm using titanium version 3.1.1 and the app works fine on iphone. Crashes on android os 4.0 and above. Let me know if you need more information on this question. Thanks in advance.

var xhr = Ti.Network.createHTTPClient();
    xhr.setTimeout(10000);
 
    xhr.open("GET",this.url);
 
    var parent = this;
    var parentwin = this.parent;
 
    xhr.onerror = function() {
        hideIndicator();
        alert(
            'The requested network resource is currently unavailable or is broken.\n' + 
            'Please make sure you\'re connected to a network.'
        );
    };
and here's the part of log output with exception,
W/dalvikvm( 1143): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
E/TiApplication( 1143): (main) [331,3187] Sending event: exception on thread: main msg:java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.truman.its.mobile/edu.truman.its.mobile.TrumanmobileActivity}: android.os.NetworkOnMainThreadException; Titanium 3.1.1,2013/06/15 16:10,f7592c1
E/TiApplication( 1143): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.truman.its.mobile/edu.truman.its.mobile.TrumanmobileActivity}: android.os.NetworkOnMainThreadException
E/TiApplication( 1143):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
E/TiApplication( 1143):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
E/TiApplication( 1143):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
E/TiApplication( 1143):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
E/TiApplication( 1143):     at android.os.Handler.dispatchMessage(Handler.java:99)
E/TiApplication( 1143):     at android.os.Looper.loop(Looper.java:137)
E/TiApplication( 1143):     at android.app.ActivityThread.main(ActivityThread.java:5103)
E/TiApplication( 1143):     at java.lang.reflect.Method.invokeNative(Native Method)
E/TiApplication( 1143):     at java.lang.reflect.Method.invoke(Method.java:525)
E/TiApplication( 1143):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
E/TiApplication( 1143):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/TiApplication( 1143):     at dalvik.system.NativeStart.main(Native Method)
E/TiApplication( 1143): Caused by: android.os.NetworkOnMainThreadException
E/TiApplication( 1143):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
E/TiApplication( 1143):     at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)
E/TiApplication( 1143):     at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
E/TiApplication( 1143):     at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
E/TiApplication( 1143):     at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
E/TiApplication( 1143):     at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
E/TiApplication( 1143):     at org.appcelerator.kroll.common.TiFastDev$Session.blockRead(TiFastDev.java:310)
E/TiApplication( 1143):     at org.appcelerator.kroll.common.TiFastDev$Session.readInt(TiFastDev.java:349)
E/TiApplication( 1143):     at org.appcelerator.kroll.common.TiFastDev$Session.readTokenCount(TiFastDev.java:412)
E/TiApplication( 1143):     at org.appcelerator.kroll.common.TiFastDev.openInputStream(TiFastDev.java:214)
E/TiApplication( 1143):     at org.appcelerator.kroll.util.KrollAssetHelper.readAsset(KrollAssetHelper.java:53)
E/TiApplication( 1143):     at org.appcelerator.titanium.TiLaunchActivity.loadActivityScript(TiLaunchActivity.java:103)
E/TiApplication( 1143):     at org.appcelerator.titanium.TiLaunchActivity.windowCreated(TiLaunchActivity.java:151)
E/TiApplication( 1143):     at org.appcelerator.titanium.TiRootActivity.windowCreated(TiRootActivity.java:109)
E/TiApplication( 1143):     at org.appcelerator.titanium.TiBaseActivity.onCreate(TiBaseActivity.java:527)
E/TiApplication( 1143):     at org.appcelerator.titanium.TiLaunchActivity.onCreate(TiLaunchActivity.java:144)
E/TiApplication( 1143):     at org.appcelerator.titanium.TiRootActivity.onCreate(TiRootActivity.java:97)
E/TiApplication( 1143):     at android.app.Activity.performCreate(Activity.java:5133)
E/TiApplication( 1143):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
E/TiApplication( 1143):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
E/TiApplication( 1143):     ... 11 more
E/AndroidRuntime( 1143): FATAL EXCEPTION: main
E/AndroidRuntime( 1143): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.truman.its.mobile/edu.truman.its.mobile.TrumanmobileActivity}: android.os.NetworkOnMainThreadException
E/AndroidRuntime( 1143):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
E/AndroidRuntime( 1143):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
E/AndroidRuntime( 1143):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
E/AndroidRuntime( 1143):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
E/AndroidRuntime( 1143):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 1143):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 1143):    at android.app.ActivityThread.main(ActivityThread.java:5103)
E/AndroidRuntime( 1143):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1143):    at java.lang.reflect.Method.invoke(Method.java:525)
E/AndroidRuntime( 1143):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
E/AndroidRuntime( 1143):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime( 1143):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 1143): Caused by: android.os.NetworkOnMainThreadException
E/AndroidRuntime( 1143):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
E/AndroidRuntime( 1143):    at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)
E/AndroidRuntime( 1143):    at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
E/AndroidRuntime( 1143):    at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
E/AndroidRuntime( 1143):    at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
E/AndroidRuntime( 1143):    at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
E/AndroidRuntime( 1143):    at org.appcelerator.kroll.common.TiFastDev$Session.blockRead(TiFastDev.java:310)
E/AndroidRuntime( 1143):    at org.appcelerator.kroll.common.TiFastDev$Session.readInt(TiFastDev.java:349)
E/AndroidRuntime( 1143):    at org.appcelerator.kroll.common.TiFastDev$Session.readTokenCount(TiFastDev.java:412)
E/AndroidRuntime( 1143):    at org.appcelerator.kroll.common.TiFastDev.openInputStream(TiFastDev.java:214)
E/AndroidRuntime( 1143):    at org.appcelerator.kroll.util.KrollAssetHelper.readAsset(KrollAssetHelper.java:53)
E/AndroidRuntime( 1143):    at org.appcelerator.titanium.TiLaunchActivity.loadActivityScript(TiLaunchActivity.java:103)
E/AndroidRuntime( 1143):    at org.appcelerator.titanium.TiLaunchActivity.windowCreated(TiLaunchActivity.java:151)
E/AndroidRuntime( 1143):    at org.appcelerator.titanium.TiRootActivity.windowCreated(TiRootActivity.java:109)
E/AndroidRuntime( 1143):    at org.appcelerator.titanium.TiBaseActivity.onCreate(TiBaseActivity.java:527)
E/AndroidRuntime( 1143):    at org.appcelerator.titanium.TiLaunchActivity.onCreate(TiLaunchActivity.java:144)
E/AndroidRuntime( 1143):    at org.appcelerator.titanium.TiRootActivity.onCreate(TiRootActivity.java:97)
E/AndroidRuntime( 1143):    at android.app.Activity.performCreate(Activity.java:5133)
E/AndroidRuntime( 1143):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
E/AndroidRuntime( 1143):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
E/AndroidRuntime( 1143):    ... 11 more
W/ActivityManager(  285):   Force finishing activity edu.truman.its.mobile/.TrumanmobileActivity

Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>