0

I already checked a lot of same questions on StackOverflow, but I didn't find any solution to my issue.
In a DialogFragment, I call an AsyncTask method and when the result has been received from the server, I launched another DialogFragment.
Here is the code I use to launch the DialogFragment :

public class RequesterConfirmRent extends DialogFragment {

// Called from onPostExecute() in AsyncTask class.
public void onPostComputeAmountToPay(JSONArray array){
        double amountToPay = 0.0;
        String ownerName = "";
        try{
            if(!array.getJSONObject(0).getBoolean("success"))
                Log.e("Error", "Error with JSON received");
            else {
                amountToPay = array.getJSONObject(1).getDouble("amountToPay");
                ownerName   = array.getJSONObject(2).getString("ownerName");
            }
        }catch(JSONException e){
            Log.e(e.getClass().getName(), "JSONException", e);
        }

        // Create and show the DialogFragment
        Paiement p = new Paiement();
        Bundle bdl = new Bundle();
        bdl.putString("ownerName", ownerName);
        bdl.putDouble("amountToPay", amountToPay);
        p.setArguments(bdl);

        // Buggy line (NPE)
        p.show(getFragmentManager(), "4554");
    }
}

And here is the code of the DialogFragment I try to display:

public class Paiement extends DialogFragment {

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        if(getDialog() == null)
            super.setShowsDialog(false);
        Dialog dialog =  super.onCreateDialog(savedInstanceState);
        dialog.setTitle("Synthesis of your rent");
        return dialog;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_paiement, container, false);
        init(rootView);
        return rootView;
    }

   private void init(View v){// Bla bla ...}
}

And I alway got a NullPointerException when I call the .show() method?

What did I do wrong?

Many thanks for your help!

EDIT 1 : As requested, here is the LogCat

05-11 09:58:34.470 31384-31384/com.example.celien.drivemycar E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NullPointerException at android.support.v4.app.DialogFragment.show(DialogFragment.java:136) at com.example.celien.drivemycar.fragment.RequesterConfirmRent.onPostComputeAmountToPay(RequesterConfirmRent.java:148)

EDIT 2 I modified the code like this, and it appears that getFragmentManager() is null. Why?

        Paiement p = new Paiement();
        Bundle bdl = new Bundle();
        bdl.putString("ownerName", ownerName);
        bdl.putDouble("amountToPay", amountToPay);
        p.setArguments(bdl);
        // BUGGY LINE
        android.support.v4.app.FragmentManager f = getFragmentManager();
        if(p == null)
            Log.d("Exception ", "p is null");
        if(f == null)
            Log.d("Exception ", "f is null");

        try {
            p.show(f, "4554");
        }catch(NullPointerException e){
            Log.d("Exception  ", e.toString());
        }

EDIT 3: Got some fresh infos!
To avoid the creation of this Dialog, I display data in a Toast:
Toast.makeText(this.getActivity(), "You have to pay "+amountToPay+"e to " +ownerName, Toast.LENGTH_LONG).show(); and also get a NPE!
BUT if I use the Log system, everything's fine :

Log.d("Rcvd ", String.valueOf(amountToPay));
Log.d("Rcvd ", ownerName);

So, why is my activity null?

Mornor
  • 3,471
  • 8
  • 31
  • 69

1 Answers1

2

If you get a NPE when calling p.show() but not p.setArguments(), it could be that p is ok but something inside the show call isn't?

On possible point to solve is that you're using a support version of FragmentManager, with the getFragmentManager() call. Try the getSupportFragmentManager() instead. It will fall back to the proper one when needed. On the other hand, you're calling android.support.v4.app.FragmentManager. If you manually added the package, it's weird, so chances are your IDE did it for you. You could try to remove the package behind FragmentManager, and hope for the code be compliant to the non-support standard framework. Could be that only this reference to the support library is done, so removing the package part would solve the issue.

My advice: In an app, always stick to either the standard framework or the support library when defining activities and fragments. Because of that, make sure that every Activity and Fragment you create extends a proper support (or standard framework) version. Mixing them will end up with unexpected crashes.

Also, as mentioned in one of my comments, AsyncTask runs freely even after your fragment was detached, so no activity is properly referenced by this fragment anymore. This answer tells you to check if Fragment was detached by looking at isDetached(). Check for his answer. He's talking about using Loaders instead of AsyncTasks or move the AsyncTask up to the activity, so the activity is always available. Looking at the future, Loader is the best option (since it's the natural evolution of AsyncTask), looking at the present, try to move the AsyncTask up to the common Activity.

Community
  • 1
  • 1
Sergi Juanola
  • 6,531
  • 8
  • 56
  • 93
  • So I call the supportFragmentManager() : `FragmentManager f = getActivity().getSupportFragmentManager();` and `import android.support.v4.app.FragmentManager;`. But got a NPE at the creation of f. – Mornor May 11 '15 at 08:48
  • As long as you use v4's versions of Fragment, FragmentManager, FragmentTransaction and so, yes. You should pay attention to that. Otherswise, you may find out later that the app is not properly working on some Android devices. Hopefully the code will gratefully crash because of this before deploying, but it's always a good idea to ensure you use the support library across the app. – Sergi Juanola May 11 '15 at 08:50
  • Thanks for this advice! But don't know what to do to fix it? – Mornor May 11 '15 at 08:53
  • @Mornor I'm pretty sure that, if it doesn't solve it (reference of somebody with the same issue [here](http://stackoverflow.com/questions/20527417/android-getfragmentmanager-returns-null), seriously, use the `Support` infix everywhere, and extend the support versions of activities and fragments as well), chances are the `AsyncTask` is messing with the activity. – Sergi Juanola May 11 '15 at 09:02
  • Seems like I'm lucky today ... I edited my code with new uselful infos. Could this help you? – Mornor May 11 '15 at 09:18
  • @Mornor Did you try to remove the `android.support.v4.app.` part, or change everything to use the support library? Update the code according, if you did, or give it a try if you didn't. I also commented your question – Sergi Juanola May 11 '15 at 09:21
  • I tried to remove support v4 library but not the other option yet. Unfortunately, I have to go and will be back as soon as I can :( – Mornor May 11 '15 at 09:27