使用DialogFragment NullPointerException异常调用活动 [英] NullPointerException calling activity using DialogFragment
问题描述
我在我的转换对话框以 DialogFragments
的过程。我下面的说明这里但看起来他们是在活动使用它
,我用它在片段
。我创建了监听器
回到我的片段,但是当我打电话 getActivity()
则抛出一个 NullPointerException异常
:
公共类DialogTextAdd扩展DialogFragment实现OnEditorActionListener {
私人的EditText行文字;
公共接口DialogTextAddListener {
无效onDialogTextAdd(最后弦乐的inputText);
}
公共DialogTextAdd(){
//需要DialogFragment空的构造
}
@覆盖
公共查看onCreateView(最终LayoutInflater吹气,最后的ViewGroup容器中,最后包savedInstanceState){
最终的视图中查看= inflater.inflate(R.layout.dialog_edit,集装箱);
多行文字=(EditText上)view.findViewById(R.id.Text_add);
getDialog()的setTitle(添加文本)。
//自动显示软键盘
mText.requestFocus();
getDialog()getWindow()setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE)。
mText.setOnEditorActionListener(本);
返回查看;
}
@覆盖
公共布尔onEditorAction(最终的TextView V,最终诠释actionId,最终KeyEvent的事件){
如果(EditorInfo.IME_ACTION_DONE == actionId){
新MyFragment()onDialogTextAdd(mText.getText()的toString()); //觉得这是我的问题
this.dismiss();
返回true;
}
返回false;
}
}
公共类MyFragment扩展SherlockListFragment实现DialogKeywordAddListener
{
@覆盖
公共无效onDialogTextAdd(最终文本字符串){
Toast.makeText(getActivity(),文字+保存,Toast.LENGTH_SHORT).show();
}
@覆盖
公共无效onAttach(活动行为){
super.onAttach(行为);
}
}
使用这个code得到它的工作:
MyFragment MF =新MyFragment();
捆绑的args =新包();
args.putString(文本,mText.getText()的toString());
mf.setArguments(参数);
。getActivity()getSupportFragmentManager()的BeginTransaction()加(MF,my_fragment)提交()。;
如果你不使用 getSupportFragmentManager()
并添加(。新增(片段,标签)
)的片段到 FragmentTransaction
的通过执行新的实例 .beginTransaction()
,然后调用提交()
?我想这就是你正在努力做的事情。
getActivity()
返回空
,因为新片段实例未连接到活动(本机 FragmentActivity /活动
或 SherlockFragmentActivity / SherlockActivity
,不管它是什么)。
做关于使用部分片段阅读。该片段的Instantiantion是不够的。查找有关实例化后的基本操作。
I am in the process of converting my dialogs to DialogFragments
. I'm following the instructions here but it looks like they are using it in an Activity
and I'm using it in a Fragment
. I've created the Listener
back to my fragment, but when I call getActivity()
it is throwing a 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);
}
}
Using this code got it to work:
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();
Shouldn't you use getSupportFragmentManager()
and add (.add(fragment, tag)
) the new instance of your fragment to the FragmentTransaction
by doing .beginTransaction()
and then call commit()
? I guess thats what you are trying to do.
getActivity()
returns null
since your new Fragment instance is not attached to the Activity (native FragmentActivity/Activity
or SherlockFragmentActivity/SherlockActivity
, whatever it is).
Do some readings about using fragments. Instantiantion of the fragment is not enough. Find about underlying operations after instantiation.
这篇关于使用DialogFragment NullPointerException异常调用活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!