DialogFragment getActivity()在片段处理其他数据后返回null

时间:2016-10-05 06:37:58

标签: android android-fragments android-activity firebase android-dialogfragment

我看过很多getActivity()返回null问题,但我似乎无法找到符合此情况的问题。

我有一个DialogFragment应该执行以下操作:

  • 从用户那里获取输入
  • 验证输入是否正确,如果是,则仅进行/解除对话
  • 向Firebase数据库进行交易/写入数据(这需要一秒左右)
  • 转到新活动并完成通话活动

我可以让所有这些部分独立工作,但是如果我在FB事务完成后将代码移到新活动中,那么当我设置startActivity()意图时,我会得到一个NPE。 / p>

据我所知,在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 个答案:

答案 0 :(得分:1)

  

据我所知,在Firebase事务发生期间,AlertDialog和DialogFragment会自动被解除,我无法再获取上下文来设置我的意图。

AFAIK这是确切的原因。

解决方案1 ​​

将经过验证的数据传递给托管Activity并在那里执行firebase写入。这样,您将始终拥有上下文来启动另一项活动。

解决方案2

在完成firebase写入并启动Dialog之前,请不要忽略ProgressBar(显示ConversationListActivity)。