I v got a gridview and i m populating items with a ASyncTask "inner" class. My innerclass's parent class is extending fragment. And off course this fragment in the main activity for swiping pages. I must send activity to inner class for progressDialog and other things. I try some words for this but, every time im getting NullPointerException error in the logcat. Please Help me guys. All Fragment class codes are just below;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
/**
* A simple {@link Fragment} subclass.
*/
public class FragmentB extends Fragment {
public FragmentB() {
// Required empty public constructor
}
private ProgressDialog pDialog;
private String url = "http://**********.com/api.asp?cmd=resdok&gore=tarih&kategori=all";
//JSON NODLAR
public static final String POST_ID = "id";
public static final String FOTOYOL = "img";
ArrayList<HashMap<String, String>> WallPaperList = new ArrayList<HashMap<String, String>>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_b, container, false);
if (isConnected()){
**// HERE IMPORTANT I THINK !**
new ProgressTask(**v.getContext()**).execute();
}
else
{
Toast.makeText(v.getContext(),"Please check your internet connection..!",Toast.LENGTH_LONG).show();
}
/*
GridView g = (GridView)v.findViewById(R.id.gridview);
g.setAdapter(new AdapterB(v.getContext(),WallPaperList));
*/
return v;
}
public boolean isConnected(){
ConnectivityManager connMgr = (ConnectivityManager) getActivity().getSystemService(Activity.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
return true;
else
return false;
}
private class ProgressTask extends AsyncTask<String,Void,Boolean>{
private ProgressDialog progressDialog;
private Context mContext;
final AdapterB adapter = new AdapterB(mContext,WallPaperList);
public ProgressTask(Activity activity)
{
**//AND HERE !!**
this.mContext = activity;
progressDialog = new ProgressDialog(mContext);
}
protected void onPreExecute() {
this.progressDialog.setMessage("Please Wait");
this.progressDialog.setCancelable(false);
this.progressDialog.show();
}
@Override
protected void onPostExecute(final Boolean success)
{
if (progressDialog.isShowing())
{
progressDialog.dismiss();
}
}
protected Boolean doInBackground(final String... args)
{
JSONParser jsonParser = new JSONParser();
JSONArray json = jsonParser.getJSONArrayFromUrl(url);
for (int i = 0;i < json.length(); i++)
{
try
{
JSONObject c = json.getJSONObject(i);
String image = c.getString(FOTOYOL);
String id = c.getString(POST_ID);
HashMap<String,String> map = new HashMap<String,String>();
map.put(POST_ID, id);
map.put(FOTOYOL, image);
WallPaperList.add(map);
}
catch (JSONException e)
{
e.printStackTrace();
}
}
return null;
}
}
}