0

我的应用程序中有一个对话框片段,我正在调用它 -

    dialog=new
     FragmentActivity.dfragment(this);

    getFragmentManager().findFragmentById(R.id.fragment3).getView()
            .findViewById(R.id.btn1)
            .setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                                             dialog.show(getFragmentManager(), "Dialog");
                                             System.out.println(getFragmentManager().findFragmentByTag("Dialog").getTag());
                     // gives null pointer exception
                }
            });

// 对话框片段类。

public static class dfragment extends DialogFragment {
    public dfragment(DialogDisplayed dd) {
        super();
        this.dd = dd;
        Bundle b = new Bundle();
        b.putBoolean("Value", true);
        setArguments(b);
    }

    DialogDisplayed dd;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        getDialog().setTitle("DIALOG FRAGMENT");
        View v = inflater.inflate(R.layout.dialog_layout, container);
        v.findViewById(R.id.diss).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(), "Setting the bundle",
                        Toast.LENGTH_SHORT).show();

                dismiss();
                dd.onDialogClicked();
            }
        });
        return v;
    }

}

我相信它无法通过标签“对话框”找到片段,即使文档说它将它添加到 FragManager。我想知道为什么它会例外谢谢

4

1 回答 1

0

你有空引用异常原因

dialog.show(getFragmentManager(), "对话框");

不会立即将您的片段添加到 fragmentManager,而是创建 FragmentTransaction 并安排提交。当 UI 线程准备好时,将执行此提交。有关详细信息,请参阅http://developer.android.com/reference/android/app/FragmentTransaction.html#commit()

于 2013-04-10T17:59:43.767 回答