Android 使用DialogFragment调用NullPointerException活动

Android 使用DialogFragment调用NullPointerException活动,android,android-dialogfragment,Android,Android Dialogfragment,我正在将对话框转换为对话框片段。我正在按照说明进行操作,但看起来他们正在活动中使用它,而我正在片段中使用它。我已经创建了侦听器返回到我的片段,但是当我调用getActivity()时,它抛出了一个NullPointerException: public class DialogTextAdd extends DialogFragment implements OnEditorActionListener { private EditText mText; public int

我正在将对话框转换为
对话框片段
。我正在按照说明进行操作,但看起来他们正在
活动中使用它,而我正在
片段中使用它。我已经创建了
侦听器
返回到我的片段,但是当我调用
getActivity()
时,它抛出了一个
NullPointerException

public class DialogTextAdd extends DialogFragment implements OnEditorActionListener {

    private EditText mText;

    public interface DialogTextAddListener {
        void onDialogTextAdd(final String inputText);
    }

    public DialogTextAdd() {
        // Empty constructor required for DialogFragment
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.dialog_edit, container);
        mText = (EditText)view.findViewById(R.id.Text_add);
        getDialog().setTitle("Add Text");

        // Show soft keyboard automatically
        mText.requestFocus();
        getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        mText.setOnEditorActionListener(this);

        return view;
    }

    @Override
    public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event) {
        if (EditorInfo.IME_ACTION_DONE == actionId) {

            new MyFragment().onDialogTextAdd(mText.getText().toString()); //think this is my problem
            this.dismiss();
            return true;
        }
        return false;
    }
}

public class MyFragment extends SherlockListFragment implements DialogKeywordAddListener
{
    @Override
    public void onDialogTextAdd(final String text) {
        Toast.makeText(getActivity(), text + " saved", Toast.LENGTH_SHORT).show();
    }


@Override
public void onAttach(Activity act) {
    super.onAttach(act);
}


}
使用此代码使其工作:

    MyFragment mf = new MyFragment();
    Bundle args = new Bundle();
    args.putString("text", mText.getText().toString());
    mf.setArguments(args);

    getActivity().getSupportFragmentManager().beginTransaction().add(mf, "my_fragment").commit();
使用:

而不是:

getActivity()
编辑:

使用这种方法

@Override
public void onAttach(SupportActivity activity) {
    super.onAttach(activity);
}

您是否应该使用
getSupportFragmentManager()
并通过执行
.beginTransaction()
然后调用
commit()
将(
.add(fragment,tag)
)片段的新实例添加到
FragmentTransaction
?我想这就是你想要做的

getActivity()
返回
null
,因为新片段实例未附加到活动(原生
FragmentActivity/Activity
SherlockFragmentActivity/SherlockActivity
,无论它是什么)


做一些关于使用碎片的阅读。片段的实例化是不够的。查找实例化后的底层操作

调用时,
getActivity()
是否为空?是的……(必须添加这些字符以填充12个字符)每次调用
onEditorAction()
时,您都会不断生成一个新片段。而你的新片段所做的就是干杯。这个子片段有多大必要?在
onDialogTextAdd
(一些数据库调用)中有更多的内容,但我没有提到。不确定,我只是在关注谷歌关于
对话片段的文档,谢谢你的回复。不幸的是,这不起作用,因为
getActivity()
为空,因此对其调用
getApplicationContext()
会导致另一个NPE。谢谢。实际上我已经有了,但没有包括在内。我会更新我的OP@KrisB尝试在那里使用
SupportActivity
进行更改。我仍然在思考
片段
,从名称来看这可能是显而易见的,但是
对话框片段
的处理方式就像它自己的
片段
?是的,你的权利。SDK DialogFragment实现仍然使用fragmentmanager来显示/取消片段(对话框)。
@Override
public void onAttach(SupportActivity activity) {
    super.onAttach(activity);
}