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);
}
我在下面看到链接:
Load More - Only the original thread that created a view hierarchy can touch its views
android - Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException
每个人都说
您正在尝试更新您的主UI线程或AsyncTask永远不允许这样做。
然后我可以更新或显示我的图片。
还可以尝试 AsyncTask 。 比如何实施循环
答案 0 :(得分:1)
答案 1 :(得分:1)
使用此代码它会有帮助,
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
答案 2 :(得分:0)
您正在UI线程中调用Web服务。 Android不允许您在API&gt; = 3.0的UI线程中执行网络操作。
解决方案:
将图像加载到其他线程中。
new Thread()
{
@Override
public void run()
{
// load image here
super.run();
}
}.start();
使用LazyList可以很好地解决此问题。
您也可以使用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
}
}