-6
Caused by: android.os.NetworkOnMainThreadException
04-25 13:15:52.362: E/AndroidRuntime(20900):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
    04-25 13:15:52.362: E/AndroidRuntime(20900):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
    04-25 13:15:52.362: E/AndroidRuntime(20900):    at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
    04-25 13:15:52.362: E/AndroidRuntime(20900):    at libcore.io.IoBridge.connect(IoBridge.java:112)
    04-25 13:15:52.362: E/AndroidRuntime(20900):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
    04-25 13:15:52.362: E/AndroidRuntime(20900):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
    04-25 13:15:52.362: E/AndroidRuntime(20900):    at java.net.Socket.connect(Socket.java:842)
    04-25 13:15:52.362: E/AndroidRuntime(20900):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
    04-25 13:15:52.362: E/AndroidRuntime(20900):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
    04-25 13:15:52.362: E/AndroidRuntime(20900):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
    04-25 13:15:52.362: E/AndroidRuntime(20900):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
    04-25 13:15:52.362: E/AndroidRuntime(20900):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
    04-25 13:15:52.362: E/AndroidRuntime(20900):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
    04-25 13:15:52.362: E/AndroidRuntime(20900):    at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
    04-25 13:15:52.362: E/AndroidRuntime(20900):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
    04-25 13:15:52.362: E/AndroidRuntime(20900):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
    04-25 13:15:52.362: E/AndroidRuntime(20900):    at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
    04-25 13:15:52.362: E/AndroidRuntime(20900):    at java.net.URLConnection.getContent(URLConnection.java:190)
    04-25 13:15:52.362: E/AndroidRuntime(20900):    at java.net.URL.getContent(URL.java:447)
    04-25 13:15:52.362: E/AndroidRuntime(20900):    at biz.xicom.printvintage.HomeScreen$2.run(HomeScreen.java:199)
    04-25 13:15:52.362: E/AndroidRuntime(20900):    at android.app.Activity.runOnUiThread(Activity.java:4644)
    04-25 13:15:52.362: E/AndroidRuntime(20900):    at biz.xicom.printvintage.HomeScreen.setview(HomeScreen.java:193)
    04-25 13:15:52.362: E/AndroidRuntime(20900):    at biz.xicom.printvintage.HomeScreen.onCreate(HomeScreen.java:58)
    04-25 13:15:52.362: E/AndroidRuntime(20900):    at android.app.Activity.performCreate(Activity.java:5104)
    04-25 13:15:52.362: E/AndroidRuntime(20900):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    04-25 13:15:52.362: E/AndroidRuntime(20900):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

code

    for (int i = 0; i < image_list.size(); i++) {
        LayoutInflater layoutInflator = HomeScreen.this.getLayoutInflater();
        LinearLayout childlayout = (LinearLayout) layoutInflator.inflate(
                R.layout.image_text_web_services_inflate, mPager, false);
        im = (ImageView) childlayout.findViewById(R.id.image_web_service);
        t = (TextView) childlayout.findViewById(R.id.text_web_service);

        t.setText(HomeScreen.detail_list.get(i));
        final int j=i;
        runOnUiThread(new Runnable() {
            public void run(){   
                try {
                    Bitmap bitmap = BitmapFactory
                            .decodeStream((InputStream) new URL(
                                    HomeScreen.image_list.get(j))
                                    .getContent());
                    im.setImageBitmap(bitmap);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        mPager.addView(childlayout);
    }

i read below link:

Load More - Only the original thread that created a view hierarchy can touch its views

android - Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException

Every one said

You are trying to update your Main UI Thread or AsyncTask never allowed that.

then where i can update or show my image.

Also try AsyncTask. than how to implement on loop

Community
  • 1
  • 1
duggu
  • 37,851
  • 12
  • 116
  • 113
  • Make and asynctask that in background fetches the bitmap and onPostExecute sets it to your ImageView. Also incorporate caching is another tip. There are loads of examples out there. – David Olsson Apr 25 '13 at 13:26
  • @DavidOlsson im on loop so how can i send loop value to onPostExecute. – duggu Apr 25 '13 at 13:28
  • 1
    @IceMAN It's not possible duplicate, here issue is somewhat different. – Paresh Mayani Apr 25 '13 at 13:29
  • @HCD, take a read over at http://developer.android.com/training/displaying-bitmaps/process-bitmap.html – David Olsson Apr 25 '13 at 13:29
  • @IceMAN i now question is repeated i m not satisfying those answer and will u read my question properly. – duggu Apr 25 '13 at 13:30
  • @HCD: No need to be rude fella! If my flag is wrong, my apologies. The other users will ignore it if everyone agrees it is wrongly marked duplicate. – Siddharth Lele Apr 25 '13 at 13:32
  • Create a solution for one iteration in the loop and use it for all. Handle the Network stuff in a background thread (Just like all the google answers says and the exception). And then update the ImagView in onPostExecute. – David Olsson Apr 25 '13 at 13:33
  • @IceMAN the thing is nobody can read whole question they just go only for heading thats my bad luck !!! – duggu Apr 25 '13 at 13:35

3 Answers3

1

As you are trying to load Images inside Loop, I would suggest you to implement logic of Lazy loading of Images.

Here are some libraries you can give a try:

  1. Lazy List
  2. Universal Image Loader for Android
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
1

Use this code It will help,

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
No_Rulz
  • 2,679
  • 1
  • 20
  • 33
0

You are calling a web service in UI thread. Android does not allow you to perform network operation in UI thread from API>=3.0.

Solutions:

  1. Load images in a different thread.

    new Thread()
    {
         @Override
         public void run() 
         {
            // load image here
            super.run();
         }
    }.start();
    
  2. Using LazyList would be a great solution for this problem.

  3. You may use AsyncTask too.

    class MyAsyncTask extends AsyncTask<String, String, String> {
    
    @Override
    protected void onPreExecute() {
     // Runs on UI thread- Any code you wants 
     // to execute before web service call. Put it here.
     // Eg show progress dialog
    }
    @Override
    protected String doInBackground(String... params) {
      // Runs in background thread
        String result = //your web service request;         
    
         return result;
    }
    
    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
    }
    
    @Override
    protected void onPostExecute(String resp) {
    
      // runs in UI thread - You may do what you want with response 
      // Eg Cancel progress dialog - Use result         
    }
    }
    
duggu
  • 37,851
  • 12
  • 116
  • 113
Adnan
  • 5,025
  • 5
  • 25
  • 44