2
我正在使用畢加索顯示從jQuery中分析的一堆圖像在gridview中,但在旋轉應用程序崩潰和postexecute getActivity()變爲null!onRotate getactivity爲null
我的片段:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_upload_data, container, false);
images_gv=(GridView) v.findViewById(R.id.images);
new RetrieveItemData().execute();
return v;
}
這裏是我的片段RetieveItemData類:
private class RetrieveItemData extends AsyncTask<Void, Void, Void> {
private ArrayList<String> imageList=new ArrayList<String>();
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(Constant.URL, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray images;
if(!jsonObj.isNull(TAG_IMAGES)) {
images=jsonObj.getJSONArray(TAG_IMAGES);
//looping through Features
for (int i = 0; i < images.length(); i++) {
imageList.add(images.getString(i));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
imageAdapter=new GridViewAdapter(getActivity().getApplicationContext(), imageList);
images_gv.setAdapter(imageAdapter);
}
}
,這裏是我的適配器:
public class GridViewAdapter extends BaseAdapter {
final Context context;
private List<String> urls = new ArrayList<String>();
public GridViewAdapter(Context context,ArrayList<String> urls) {
this.context = context;
this.urls=urls;
}
@Override public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.gridview_image_item, null);
}
ImageView imageview = (ImageView) convertView.findViewById(R.id.image_item);
// Get the image URL for the current position.
String url = getItem(position);
// Trigger the download of the URL asynchronously into the image view.
Picasso.with(context) //
.load(url) //
.fit()
.into(imageview);
return convertView;
}
@Override public int getCount() {
return urls.size();
}
@Override public String getItem(int position) {
return urls.get(position);
}
@Override public long getItemId(int position) {
return position;
}
final class SquaredImageView extends ImageView {
public SquaredImageView(Context context) {
super(context);
}
public SquaredImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());
}
}
感謝,它不崩潰了,但progressdialog永遠不會被關閉! – 2014-10-06 02:37:36