2014-10-05 41 views
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()); 
     } 
    } 

回答

2

是執行任務,這個片段沒有再附加一個活動。當設備的配置發生變化時(例如更改設備的方向),整個活動將被銷燬並重新創建,因此係統正在創建一個全新的活動(並附加一個全新的Fragment)。

最簡單的解決方案是取消任務,如果碎片被銷燬。新創建的Fragment將再次啓動該任務。這意味着你可能會建立兩次網絡連接,所以可能會有更高效的數據使用方式,但這並不算太壞,因爲在數據加載時,這種情況不太可能發生。

您可以通過調整你的片段,像這樣實現這一點:

RetrieveItemData mRetrieveDataTask; 

@Override 
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); 

    mRetrieveDataTask = new RetrieveItemData(); 
    mRetrieveDataTask.execute(); 

    return v; 
} 

@Override 
public void onDestroyView(){ 
    super.onDestroyView(); 
    mRetrieveDataTask.cancel(false); 
} 
+0

感謝,它不崩潰了,但progressdialog永遠不會被關閉! – 2014-10-06 02:37:36

相關問題