我正在创建一个android工作室应用程序,它的一个标签中有一个rss feed,现在添加了图像和位图代码,我尝试将我的图像放到imageview,每当我运行时,我的适配器我的代码崩溃了:
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
at com.example.pauly.myapplication.LAdapter.ImgBM(LAdapter.java:54)
at com.example.pauly.myapplication.LAdapter.getView(LAdapter.java:41)
at android.widget.AbsListView.obtainView(AbsListView.java:2347)
at android.widget.ListView.makeAndAddView(ListView.java:1864)
at android.widget.ListView.fillDown(ListView.java:698)
at android.widget.ListView.fillFromTop(ListView.java:759)..
部分Ladapter.class:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Info info = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.basic_list_item, parent, false);
}
// Lookup view for data population
TextView title = (TextView) convertView.findViewById(R.id.Tit);
ImageView image = (ImageView) convertView.findViewById(R.id.img);
//TextView description = (TextView) convertView.findViewById(R.id.Desc);
title.setText(info.Title);
#l.41 image.setImageBitmap(ImgBM(info.Image));
//description.setText(info.Description);
// Return the completed view to render on screen
return convertView;
}
private Bitmap ImgBM(String url) {
Bitmap bm = null;
try {
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
#l.54 conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (IOException e) {
Log.e("Bitm", "Error getting bitmap", e);
}
return bm;
}
}
答案 0 :(得分:1)
问题是。NetworkOnMainThreadException
这意味着您在UI上调用网络。尝试使用AsynkTask下载图像。
将此ImgBM(url)
方法移至AsynkTask
。