0

I have a LogCat error and I don't understand it. The app was working great but then it randomly happened. I tried adding adMob to my app and then got this error so I deleted all the content for the add mob but am still getting this error! What does it mean?

09-30 22:05:33.299: E/AndroidRuntime(30293): FATAL EXCEPTION: main
09-30 22:05:33.299: E/AndroidRuntime(30293): android.os.NetworkOnMainThreadException
09-30 22:05:33.299: E/AndroidRuntime(30293):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1178)
09-30 22:05:33.299: E/AndroidRuntime(30293):    at java.net.InetAddress.lookupHostByName(InetAddress.java:394)
09-30 22:05:33.299: E/AndroidRuntime(30293):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:245)
09-30 22:05:33.299: E/AndroidRuntime(30293):    at java.net.InetAddress.getAllByName(InetAddress.java:220)
09-30 22:05:33.299: E/AndroidRuntime(30293):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
09-30 22:05:33.299: E/AndroidRuntime(30293):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
09-30 22:05:33.299: E/AndroidRuntime(30293):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
09-30 22:05:33.299: E/AndroidRuntime(30293):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
09-30 22:05:33.299: E/AndroidRuntime(30293):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:590)
09-30 22:05:33.299: E/AndroidRuntime(30293):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:510)
09-30 22:05:33.299: E/AndroidRuntime(30293):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:488)
09-30 22:05:33.299: E/AndroidRuntime(30293):    at library.JSONParser.getJSONFromUrl(JSONParser.java:42)
09-30 22:05:33.299: E/AndroidRuntime(30293):    at library.UserFunctions.loginUser(UserFunctions.java:54)
09-30 22:05:33.299: E/AndroidRuntime(30293):    at com.example.LoginActivity$1.onClick(LoginActivity.java:52)
09-30 22:05:33.299: E/AndroidRuntime(30293):    at android.view.View.performClick(View.java:3549)
09-30 22:05:33.299: E/AndroidRuntime(30293):    at android.view.View$PerformClick.run(View.java:14393)
09-30 22:05:33.299: E/AndroidRuntime(30293):    at android.os.Handler.handleCallback(Handler.java:605)
09-30 22:05:33.299: E/AndroidRuntime(30293):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-30 22:05:33.299: E/AndroidRuntime(30293):    at android.os.Looper.loop(Looper.java:154)
09-30 22:05:33.299: E/AndroidRuntime(30293):    at android.app.ActivityThread.main(ActivityThread.java:4945)
09-30 22:05:33.299: E/AndroidRuntime(30293):    at java.lang.reflect.Method.invokeNative(Native Method)
09-30 22:05:33.299: E/AndroidRuntime(30293):    at java.lang.reflect.Method.invoke(Method.java:511)
09-30 22:05:33.299: E/AndroidRuntime(30293):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-30 22:05:33.299: E/AndroidRuntime(30293):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-30 22:05:33.299: E/AndroidRuntime(30293):    at dalvik.system.NativeStart.main(Native Method)

1 Answers1

1

The Exception is NetworkOnMainThread, which means what it says: You're running code on the main thread, but you're trying to do a network operation. It looks like you're doing some HTTP related stuff, doing some JSON paring, and trying to look up a host name.

Though it often appears to us that an app was working and then randomly stopped, something we have done has caused the problem. Random bit rot occurs, but we should rule out other sources first.

You say that you tried to add adMob to your app. That's hardly "randomly happening". You "deleted the content for the adMob", but did you delete code as well? Do you have a previous version of the app that you can diff to? Did you change Android platform versions?

Your error occurs because apps are no longer allowed to do network operations on the main (aka UI thread). Network stuff has to run in the background as an asynchronous operation. For this, you can use AsyncTask, or an IntentService, or you can code your own background HandlerThread/Handler/Looper. My guess is that adding adMob put some network code into your app; I can't imagine that adMob would work without network access.

Joe Malin
  • 8,621
  • 1
  • 23
  • 18