For smart Primates & ROBOTS (oh and ALIENS ).

Blogroll

Sunday, January 18, 2015

android.os.NetworkOnMainThreadException

Problem:

The runtime this error 
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AnroidBlockGuardPolicy.onNetwork(StrictMode.java
is the common error when working with online activity work in android.
how to remove it?

Solution:

You need to put below lines to solve this:
StrictMode.ThreadPolicy thpolicy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(thpolicy);

Example:

Use below code after setContentView(..);

setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy thpolicy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(thpolicy);
}

And also use below code before  onCreate(..)

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {...}
Share:

0 comments:

Post a Comment

How to make center align child div

 How to make center align child div?   Suppose you have 2 Div tag. And want the make inner tag center <div id="d1">      ...

Ads Inside Post

Powered by Blogger.

Arsip

Blog Archive