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?