-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)

代码

    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);
    }

我阅读了以下链接:

加载更多 - 只有创建视图层次结构的原始线程才能接触其视图

android - 引起:android.view.ViewRootImpl$CalledFromWrongThreadException

每个人都说

您正在尝试更新您的主 UI 线程或 AsyncTask 从未允许这样做。

然后我可以在哪里更新或显示我的图像。

也试试AsyncTask而不是如何实现循环

4

3 回答 3

1

使用此代码会有所帮助,

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
于 2013-04-25T13:56:04.217 回答
1

当您尝试在 Loop 中加载图像时,我建议您实现图像的延迟加载逻辑。

以下是一些您可以尝试的库:

  1. 懒惰列表
  2. 适用于 Android 的通用图像加载器
于 2013-04-25T13:28:32.303 回答
0

您正在 UI 线程中调用 Web 服务。Android 不允许您在 API>=3.0 的 UI 线程中执行网络操作。

解决方案:

  1. 在不同的线程中加载图像。

    new Thread()
    {
         @Override
         public void run() 
         {
            // load image here
            super.run();
         }
    }.start();
    
  2. 使用LazyList将是解决此问题的好方法。

  3. 你也可以使用 AsyncTask。

    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         
    }
    }
    
于 2013-04-25T14:01:51.710 回答