0

Dialog class Snippet:

public class Progress {

    private Context mContext;

    /**
     * constructor
     * @param context
     */
    public Progress(Context context) {
        this.mContext = context;
    }

    /**
     * show the progress bar
     * @return
     */
    public Dialog showProgress() {
        Dialog pDialog = new Dialog(mContext);
        pDialog.setCancelable(false);
        pDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        pDialog.setContentView(new MaterialProgressBar(mContext), new ViewGroup.LayoutParams(100, 100));
        pDialog.getWindow().setBackgroundDrawable(
                new ColorDrawable(android.graphics.Color.TRANSPARENT));
        pDialog.show();
        return pDialog;
    }



    /**
     * dismiss the progress bar
     * @param pDialog
     */
    public void dismissProgress(Dialog pDialog) {
        if (pDialog != null && pDialog.isShowing()) {
            pDialog.dismiss();
        }
    }
}

In Fragment call calling dialog in onCreatemethod :

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.mContext = getActivity();       
        progress = new Progress(mContext);
    progress.showProgress();
}

Exception :

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
                                                               at android.app.Dialog.<init>(Dialog.java:175)
                                                               at android.app.Dialog.<init>(Dialog.java:149)
                                                               at com.contus.mcomm.views.Progress.showProgress(Progress.java:44)
                                                               at com.contus.mcomm.fragments.ProductListFragment.setUserVisibleHint(ProductListFragment.java:154)
                                                               at android.support.v4.app.FragmentStatePagerAdapter.setPrimaryItem(FragmentStatePagerAdapter.java:157)
                                                               at android.support.v4.view.ViewPager.populate(ViewPager.java:1270)
                                                               at android.support.v4.view.ViewPager.populate(ViewPager.java:1120)
                                                               at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1646)
                                                               at android.view.View.measure(View.java:18794)
                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)

Thanks in advance...!

snachmsm
  • 17,866
  • 3
  • 32
  • 74
Nisha
  • 45
  • 9

2 Answers2

1

SDK update may be not the issue. some times getActivity() returns null. so try to display dialog in onViewCreated() method in fragment.

For more you can check this - getActivity() returns null in Fragment function

Community
  • 1
  • 1
Wasim K. Memon
  • 5,979
  • 4
  • 40
  • 55
  • i have tried all the methods , but none of them works. – Nisha Nov 24 '16 at 13:23
  • i think , i am sure that this is happening due to version change , nut not able to find a solution for it. – Nisha Nov 24 '16 at 13:23
  • i have a class ProductListFragmentImpl extends Fragment { } and another class extends ProductListFragmentImpl where i need to show dialog. – Nisha Nov 24 '16 at 13:38
  • i have found that my onAttact is never called in fragment. – Nisha Nov 24 '16 at 14:00
  • I suggest you debug full code of both class and check weather fragment get attached or not. and if you are loading child fragment then use getChildFragmentManager(). because that will take care of attachment and detachment. – Wasim K. Memon Nov 24 '16 at 18:19
0

@androidnoobdev pointed right

getActivity() might return null in onCreate, because Fragment may be not attached to any Activity at this moment... you should move your code to onActivityCreated

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.mContext = getActivity(); //do you really need this?
}

@Override
public void onActivityCreated(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    progress = new Progress(getActivity());
    progress.showProgress();
}

if it not showing then you are not attaching Fragment properly and its parent Activity isn't present

there are also two methods:

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
}

first one, with Activity as param, was deprecated in API23 and second one was introduced in same API version. you may also move your dialog creation code here

snachmsm
  • 17,866
  • 3
  • 32
  • 74