Q:

Showing Progress Dialog in ListFragment

I have a class that extends from ListFragment.  I need to show a progress dialog since it takes some time to load the data from the server.  I am using a private class called MenuItemData and extends that class from AsyncTask.  In the onPreExecute method I have this line,
pDialog  = ProgressDialog.show(getActivity(), "Loading...", "Please wait...", false); //line 1

and in onPostExecute method,
pDialog.dismiss(); //line 2

Now when I run the program it stops and gives NullPointerException.  
But when I commented those two lines, program works fine.  I think the problem is that getActivity() returns null. 
Code for MenuItemData class
private class MenuItemData extends AsyncTask<String, String, String> {
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        try {
            if ((json_.getString(KEY_SUCCESS) ! = null)) {

                int success = Integer.parseInt(json_.getString(KEY_SUCCESS));

                if (success == 1) {

                    jsonArray = json_.getJSONObject("data").getJSONArray("items");
                    itemCount = jsonArray.length();
                    for (int i = 0; i < itemCount; i++) {
                        jsonObject = jsonArray.getJSONObject(i);
                        menuItems = new Menu_Items();
                        menuItems.setName(jsonObject.getString("name"));
                        menuItems.setDescription(jsonObject.getString("description"));
                        menuItems.setPrice("$"+jsonObject.getString("price"));
                        menu_itemsArrayList.add(menuItems);
                    }
                }
            }
        } catch (JSONException e) {
            Log.e("Menu Items2", e.toString());
        }

        pDialog.dismiss();

    }

    @Override
    protected void onPreExecute() {

        pDialog  = ProgressDialog.show(getActivity(), "Loading...", "Please wait...", false);

        super.onPreExecute();

    }

    @Override
    protected String doInBackground(String... params) {
        json_ = userFunctions.getMenuItems("Appetizers");
        return null;
    }
}

Logcat
02-05 11:25:24.245  28619-28619/com.burgerhot.main E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang. NullPointerException
        at android.app. AlertDialog.resolveDialogTheme(AlertDialog.java:142)
        at android.app. AlertDialog.<init>(AlertDialog.java:98)
        at android.app. ProgressDialog.<init>(ProgressDialog.java:77)
        at android.app. ProgressDialog.show(ProgressDialog.java:110)
        at android.app. ProgressDialog.show(ProgressDialog.java:99)
        at com.burgerhot.main. MenuCategory1$MenuItemData.onPreExecute(MenuCategory1.java:105)
        at android.os. AsyncTask.executeOnExecutor(AsyncTask.java:561)
        at android.os. AsyncTask.execute(AsyncTask.java:511)
        at com.burgerhot.main. MenuCategory1.setDataToMenu(MenuCategory1.java:61)
        at com.burgerhot.main. MenuCategory1.<init>(MenuCategory1.java:46)
        at com.burgerhot.main. MyAdapter.getItem(Menu.java:47)
        at com.burgerhot.main. MyAdapter.getItem(Menu.java:36)
        at android.support.v4.app. FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:97)
        at android.support.v4.view. ViewPager.addNewItem(ViewPager.java:832)
        at android.support.v4.view. ViewPager.populate(ViewPager.java:982)
        at android.support.v4.view. ViewPager.populate(ViewPager.java:914)
        at android.support.v4.view. ViewPager.onMeasure(ViewPager.java:1436)
        at android.view. View.measure(View.java:12911)
        at android.view. ViewGroup.measureChildWithMargins(ViewGroup.java:4805)
        at android.widget. FrameLayout.onMeasure(FrameLayout.java:297)
        at android.view. View.measure(View.java:12911)
        at android.widget. LinearLayout.measureVertical(LinearLayout.java:828)
        at android.widget. LinearLayout.onMeasure(LinearLayout.java:557)
        at android.view. View.measure(View.java:12911)
        at android.view. ViewGroup.measureChildWithMargins(ViewGroup.java:4805)
        at android.widget. FrameLayout.onMeasure(FrameLayout.java:297)
        at com.android.internal.policy.impl. PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2097)
        at android.view. View.measure(View.java:12911)
        at android.view. ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
        at android.view. ViewRootImpl.handleMessage(ViewRootImpl.java:2446)
        at android.os. Handler.dispatchMessage(Handler.java:99)
        at android.os. Looper.loop(Looper.java:137)
        at android.app. ActivityThread.main(ActivityThread.java:4448)
        at java.lang.reflect. Method.invokeNative(Native Method)
        at java.lang.reflect. Method.invoke(Method.java:511)
        at com.android.internal.os. ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
        at com.android.internal.os. ZygoteInit.main(ZygoteInit.java:590)
        at dalvik.system. NativeStart.main(Native Method)

MenuCategory1 class
public class MenuCategory1 extends ListFragment {

private UserFunctions userFunctions;
private static String KEY_SUCCESS = "success";
private JSONObject json_, jsonObject;
private JSONArray jsonArray;
int itemCount = 0;
private ProgressDialog pDialog;
private Menu_Items menuItems;
final private Context context=getActivity();
private ArrayList<Menu_Items> menu_itemsArrayList;
CustomAdapterForMenu myCustomAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

public MenuCategory1() {
    userFunctions = new UserFunctions();
    setDataToMenu();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    myCustomAdapter = new CustomAdapterForMenu(getActivity(), R.layout.menu_category1, menu_itemsArrayList);
    /** Setting the list adapter for the ListFragment */
    this.setListAdapter(myCustomAdapter);
    return super.onCreateView(inflater, container, savedInstanceState);
}

private void setDataToMenu() {
    menu_itemsArrayList = new ArrayList<Menu_Items>();
    new MenuItemData().execute();
}

//private class goes here (mentioned above)

}
So is there anyway to solve this problem? ( To show the progress dialog)
Thanks. 

A:

Try this.. 
Call like below code
new MenuItemData(getActivity()).execute();

and inside your AsyncTask use constructor
    Context context;

    public MenuItemData(Context context) {
        this.context = context;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pDialog  = ProgressDialog.show(context, "Loading...", "Please wait...", false);

    }

