Android onRotate getactivity为空
我正在使用毕加索在gridview中显示从json解析的一堆图像,但在rotate上,应用程序崩溃,postexecute中的getActivity()变为null 我的片段:Android onRotate getactivity为空,android,gridview,android-fragments,picasso,Android,Gridview,Android Fragments,Picasso,我正在使用毕加索在gridview中显示从json解析的一堆图像,但在rotate上,应用程序崩溃,postexecute中的getActivity()变为null 我的片段: public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout fo
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);
}
}
私有类RetrieveItemData扩展异步任务{
私有ArrayList imageList=新建ArrayList();
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//显示进度对话框
pDialog=newprogressdialog(getActivity());
setMessage(“请稍候…”);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…arg0){
//创建服务处理程序类实例
ServiceHandler sh=新的ServiceHandler();
//向url发出请求并获得响应
字符串jsonStr=sh.makeServiceCall(Constant.URL,ServiceHandler.GET);
Log.d(“响应:”、“>”+jsonStr);
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
JSONArray图像;
如果(!jsonObj.isNull(标记图像)){
images=jsonObj.getJSONArray(TAG_images);
//循环浏览功能
对于(int i=0;i
这是我的适配器:
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());
}
}
公共类GridViewAdapter扩展了BaseAdapter{
最终语境;
私有列表URL=new ArrayList();
公共GridViewAdapter(上下文、ArrayList URL){
this.context=上下文;
this.url=url;
}
@覆盖公共视图getView(int位置、视图转换视图、视图组父视图){
if(convertView==null){
LayoutInflater mInflater=(LayoutInflater)
context.getSystemService(Activity.LAYOUT\u INFLATER\u SERVICE);
convertView=mInflater.flate(R.layout.gridview\u image\u项,空);
}
ImageView ImageView=(ImageView)convertView.findViewById(R.id.image_项);
//获取当前位置的图像URL。
字符串url=getItem(位置);
//触发将URL异步下载到图像视图中。
毕加索。与(上下文)//
.load(url)//
.fit()
.进入(图像视图);
返回视图;
}
@重写公共int getCount(){
返回url.size();
}
@重写公共字符串getItem(int位置){
返回URL.get(位置);
}
@覆盖公共长getItemId(int位置){
返回位置;
}
最后一个类SquaredImageView扩展了ImageView{
公共SquaredImageView(上下文){
超级(上下文);
}
public SquaredImageView(上下文、属性集属性){
超级(上下文,attrs);
}
@在测量时覆盖受保护的空隙(int widthMeasureSpec、int heightMeasureSpec){
超级测量(宽度测量、高度测量);
setMeasuredDimension(getMeasuredWidth(),getMeasuredWidth());
}
}
执行任务的片段不再附加到活动。当设备的配置更改(例如更改设备的方向)时,整个活动将被销毁并重新创建,因此系统将创建一个全新的活动(并附加一个全新的片段)
最简单的解决方案是在片段被销毁时取消任务。新创建的片段将再次启动任务。这意味着您可能会进行两次网络连接,因此可能有更有效的数据使用解决方案,但这并不太糟糕,因为加载数据时不太可能发生旋转
您可以通过如下方式调整片段来实现这一点:
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);
}
谢谢,它不再崩溃,但progressdialog永远不会关闭!