0
FATAL EXCEPTION:

java.lang.NullPointerException: Attempt to invoke virtual method
   'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a
   null object reference at android.view.LayoutInflater.from(LayoutInflater.java:219)
   at com.Infoniq.restaurantapp.Fragment.HomepageFragment.Alertdailg(HomepageFragment.java:148) 
   at com.Infoniq.restaurantapp.Fragment.LoginPatternFragment$1.run(LoginPatternFragment.java:116)

LoginPatternFragment.java

if (response.contains("Successfully")) {
    final HomepageFragment hpf=new HomepageFragment();
    hpf.Alertdailg();
}

HomepageFragment.java

public void Alertdailg() {
    LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
    final View prmpview = layoutInflater.inflate(R.layout.billmode_alert, null);
    final AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
}
Larry Schiefer
  • 15,687
  • 2
  • 27
  • 33

3 Answers3

0

You're trying to call getActivity() in HomepageFragment before it is attached to activity.

You need to attach it first, using FragmentManager, like this:

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
        .beginTransaction();
HomepageFragment hpf = new HomepageFragment();
fragmentTransaction.add(R.id.container, hpf);
fragmentTransaction.commit();
hpf.Alertdailg()
IlyaGulya
  • 957
  • 6
  • 18
0

Fragment objects have a lifecycle which is tied to the Activity which is displaying it. When you call new you have just created the object but not attached it at all. You'll need to add it to the Activity using the FragmentManager:

FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
HomepageFragment hpf = new HomepageFragment();
ft.add(R.id.container, hpf);
ft.commit();

However, you still cannot call the Alertdailg() method at this point since the Fragment has still not gone through its lifecycle yet. If you always wish to display this dialog when the Fragment is first displayed, you'll need to do this in the onResume() callback of the Fragment. At that time the Activity will be attached. You can also get the Activity in the onAttach() method of the Fragment. Until that callback is executed, the Fragment is not attached.

Larry Schiefer
  • 15,687
  • 2
  • 27
  • 33
0

You need a reference of the Parent Activity context. For that first you need to create a local context variable.

private Context context;

Then get the reference of the context when your fragment gets attached to the FragmentActivity. See the code.

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

Now pass your local context variable to your AlertDialog(Context context) method and use it.

Dhrumil Shah - dhuma1981
  • 15,166
  • 6
  • 31
  • 39