Android DialogFragment getActivity()为null

时间:2014-03-18 20:47:28

标签: android nullpointerexception alertdialog dialogfragment

我正在尝试创建一个对话框,在用户注销之前向用户显示倒计时。超时是从另一个活动设置的。

我写了以下代码:

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;

public class SessionInactivityDialog extends DialogFragment {

    public void setInactivityTimeout(long timeout) {
        Resources res = getActivity().getResources();
        String text = String.format(res.getString(R.string.iminent_logout_text), (timeout / 1000));
        ((AlertDialog)getDialog()).setMessage(text);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        builder.setTitle(R.string.iminent_logout);
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                SessionActivity activity = (SessionActivity)getActivity();
                activity.resetTimer();
            }
        });

     return builder.create();
    }

}

使用以下行调用对话框:

private void showIminentLogoutDialog(long timeout) {
    mInactivityDialog.show(getFragmentManager(), TAG);
    mInactivityDialog.setInactivityTimeout(timeout);
}

即使在对话框打开后设置了超时,getActivity()中的setInactivityTimeout()仍为null

如何正确获取片段中的资源?

1 个答案:

答案 0 :(得分:3)

DialogFragment.show()是异步的 - DialogFragment实际上没有立即显示 - 它被发送到消息队列的末尾。我建议将超时值作为参数提供,然后将其与标题一起设置在onCreateDialog()中。例如:

public class SessionInactivityDialog extends DialogFragment {
    public static final String EXTRA_TIMEOUT = "timeout";

    public static SessionInactivityDialog newInstance(long timeout) {
        Bundle args = new Bundle();
        args.putLong(EXTRA_TIMEOUT, timeout);
        SessionInactivityDialog dialog = new SessionInactivityDialog();
        dialog.setArguments(args);
        return dialog;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final SessionActivity activity = (SessionActivity) getActivity();
        final long timeout = getArguments().getLong(EXTRA_TIMEOUT);
        final String msg = activity.getString(R.string.imminent_logout_text, (timeout / 1000));

        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle(R.string.imminent_logout);
        builder.setMessage(msg);
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                activity.resetTimer();
            }
        });
        return builder.create();
    }
}

然后你可以用:

显示它
SessionInactivityDialog.newInstance(timeout)
                       .show(getFragmentManager(), TAG);

我还建议为对话框创建一个与活动进行通信的界面,而不是直接依赖SessionActivity(这样你可以在任何中重复使用它活动,只要它实现你的界面)。