我已經在StackOverflow上檢查了很多相同的問題,但是我沒有找到解決我的問題的方法。
在DialogFragment
中,我調用AsyncTask
方法,當從服務器收到結果時,我啓動了另一個DialogFragment
。
這裏是我使用啓動DialogFragment
代碼:DialogFragment(支持v4)崩潰應用程序(NPE)
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");
}
}
這裏是DialogFragment的我嘗試顯示代碼:
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 ...}
}
我送花兒給人得到一個NullPointerException當我打電話了.show()
方法?
我做錯了什麼?
非常感謝您的幫助!
編輯1:按照要求,這裏是logcat的
9月5日至11日:58:34.470 31384-31384/com.example.celien.drivemycar E/AndroidRuntime:致命異常:主 顯示java.lang.NullPointerException 在android.support.v4.app.DialogFragment.show(DialogFragment.java:136) 在com.example.celien.drivemycar.fragment.RequesterConfirmRent.onPostComputeAmountToPay(RequesterConfirmRent.java:148)
編輯2我修改了這樣的代碼,看起來getFragmentManager()爲null。爲什麼?
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());
}
編輯3: 得到了一些新鮮的相關信息!
爲了避免創建此對話框,我在Toast
:Toast.makeText(this.getActivity(), "You have to pay "+amountToPay+"e to " +ownerName, Toast.LENGTH_LONG).show();
中顯示數據,並且還獲得了一個NPE!
但如果我使用日誌系統,一切都很好:
Log.d("Rcvd ", String.valueOf(amountToPay));
Log.d("Rcvd ", ownerName);
那麼,爲什麼我的活動空?
'NullPointerException'你說?發表你應該... – shkschneider
由DialogFragment稱爲DialogFragment聽起來並不像一個好主意logcat的 – Blackbelt
嗯,我想'p'是空... – shkschneider