Reputation: 3890
I need to utilize a ActionBar so I need to change the targetSdkVersion to 11. It is currently targetSdkVersion="8". It works fine with targetSdkVersion="8" , but when I change it to targetSdkVersion="11" it crashes.
The error originates at this line : imageA= new ImageAdapter(this,url);
Error:
I've tried encapsulating almost every method in an AsyncTask<> , the errors are no longer thrown but the ImageAdapter() (never called) does not work when I do this.
Here is a snippet of the code.
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.home);
title = (TextView)findViewById(R.id.title);
description = (TextView)findViewById(R.id.description);
gridView = (GridView)findViewById(R.id.propGridView);
thumbNail = (ImageView)findViewById(R.id.propertyThumbnail);
//thumbNail.setImageBitmap(bm);
gridView.setVerticalSpacing(3);
gridView.setHorizontalSpacing(3);
//checkHistory();
ImageDownloader.Mode mode = ImageDownloader.Mode.CORRECT;
historyStack.pushValue(url); // Push Value onto history stack
Log.i("No history:",url+" "+historyStack.getStack().size()); //Debug
imageA= new ImageAdapter(this,url);
imageA.getImageDownloader().setMode(mode); //Set download mode to cache
gridView.setAdapter(imageA); //Populate gridView with downloaded bitmap
}
public class ImageAdapter extends BaseAdapter {
//intialize variables
private final ImageDownloader imageDownloader ;
private int SIZE;
private List<Video> myVideos;
private Context mContext;
private String xmlurl;
private Parser parser = new Parser();
/*
* Constructor
* @params: Context
* @params: XML url
*/
public ImageAdapter(Context c, String url){
mContext = c;
xmlurl = url;
parser.runParser(xmlurl);
imageDownloader = new ImageDownloader();
}
/*
* (non-Javadoc)
* @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
*/
public View getView(final int position, View convertView, ViewGroup parent) {
final ImageView view;
if (convertView == null) {
view= new ImageView(mContext);
view.setLayoutParams(new GridView.LayoutParams(175,175));
view.setPadding(1, 1, 1, 1);
}
else{
view = (ImageView)convertView;
}
Log.i("Assingning view","");
imageDownloader.download(parser.getList().get(position).getThumbNail(),view);
return view;
}
/*
* (non-Javadoc)
* @see android.widget.Adapter#getCount()
*/
public int getCount() {
SIZE = parser.getCount();
return SIZE;
}
/*
* (non-Javadoc)
* @see android.widget.Adapter#getItem(int)
*/
public String getItem(int position) {
return myVideos.get(position).toString();
}
/*
* (non-Javadoc)
* @see android.widget.Adapter#getItemId(int)
*/
public long getItemId(int position) {
return 0;
}
public void setUrl(String url){
xmlurl = url;
}
/*
* getMethod
*/
public ImageDownloader getImageDownloader() {
return imageDownloader;
}
}
Upvotes: 0
Views: 648
Reputation: 1233
You should program the imageDownloader in threads or if you wanna run the code right now add in OnCreate:
StrictMode.setThreadPolicy(
new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().detectAll().permitNetwork().build());
this allow run your code, but isn't recommended.
Upvotes: 0
Reputation: 22306
Take a look at the exception
Android.os.NetworkOnMainThreadException
It is telling you exactly what is wrong. You are trying to perform network activity (downloading your images) on the main UI thread. In previous versions of Android this was allowed without regard to device performance. Because downloading on the UI thread will severely affect the performance of your application this is discouraged and in later versions of Android will throw the error you see above.
Move downloading your images into a separate thread and this error will go away.
Upvotes: 2