我已經閱讀了很多getActivity()
返回空問題,但我似乎無法找到與此方案匹配的問題。DialogFragment getActivity()在分片處理其他數據後返回null
我有一個DialogFragment
認爲應該做到以下幾點:
- 採取由用戶輸入
- 驗證,如果輸入的是正確的,只有繼續/駁回對話框如果是
- 進行交易/寫數據Firebase數據庫(這需要一秒左右的時間)
- 移至新活動並完成致電活動
我可以讓所有這些部分獨立工作,但是如果我在FB事務處理完成後將代碼移動到新活動中,則在設置我的startActivity()
意圖時,我會得到一個NPE。
從我所知道的情況來看,在Firebase交易發生的時候,AlertDialog和DialogFragment會自動解散,我無法再獲取上下文來設置我的意圖。
如何獲得正確的活動或上下文以致電startActivity()
? 謝謝!
public class FinishedDialog extends DialogFragment {
private String pushId;
private AlertDialog alertDialog;
public static FinishedDialog newInstance(String PushId) {
FinishedDialog frag = new FinishedDialog();
Bundle args = new Bundle();
args.putString("pushId", pushId);
frag.setArguments(args);
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
pushId = getArguments().getString("pushId");
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialog_finished, null))
.setPositiveButton(R.string.send_text_response, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// This will be overridden for data validations
}
})
.setNegativeButton(R.string.continue_without_response, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finishListening();
}
})
.setNeutralButton(R.string.listen_again, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//Dismisses Dialog
}
});
return builder.create();
}
//This is set up so that if user hasn't entered data, positiveButton won't dismiss dialog
@Override
public void onStart() {
super.onStart();
alertDialog = (AlertDialog)getDialog();
if(alertDialog != null)
{
Button positiveButton = alertDialog.getButton(Dialog.BUTTON_POSITIVE);
final EditText responseEditText = (EditText) alertDialog.findViewById(R.id.entered_response);
positiveButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Boolean wantToCloseDialog = false;
String enteredData = responseEditText.getText().toString();
if (enteredData.length()>1) {
wantToCloseDialog = true;
}
if (wantToCloseDialog) {
alertDialog.dismiss(); //This line may be part of the problem, but I still get the NPE if I comment it out.
uploadResponseToFB(enteredData);
} else {
Toast.makeText(getContext(), "You didn't enter a response!",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
private void uploadResponseToFB(String response) {
//Uploads some data to Firebase.
finishListening();
}
private void finishListeningToStory() {
//This method does a number of firebase transactions and data writes
//The goBackToMainScreen() method is called onCompletion of the last data write.
}
private void goBackToMainScreen(){
//Again, called onCompletion of last data write.
//This crashes on next line due to NPE from getActivity(), but when I test this exact code without the FB transactions (closer to onCreateDialog() or onStart()) it works fine.
Intent intent = new Intent(getActivity(), ConversationListActivity.class);
getActivity().startActivity(intent);
progressDialog.dismiss();
}
}
最初,我想做解決方案1,但我很難讓數據驗證正常工作。我會更好的去執行一個'DialogListener'的路線嗎?對於解決方案2,我嘗試顯示'ProgressDialog'微調器並保持活動狀態,直到'ConversationListActivity'開始,但我有同樣的錯誤。 – aterbo
我重構了我的代碼,將驗證的數據傳遞迴託管「活動」。這工作! – aterbo