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。 而不是如何实现循环