我刚开始使用片段。我在片段中创建了一个编辑文本,我想从活动中填充它,但它给了我空指针异常。可能是在提交fragmenttransaction之后运行OnCreateView。我已经让它以另一种方式工作,但我只是有点好奇为什么它不起作用。我搜索了它但找不到我的问题的答案。任何帮助将不胜感激
这是我的代码In activity:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Myfragment mFragment = new Myfragment();
fragmentTransaction.replace(R.id.fragment_container, mFragment);
fragmentTransaction.commit();
mFragment.set_fragment_editext("Hello world");//setting edit text in fragment throws exception
然后在片段类中:
public class Myfragment extends Fragment {
EditText edittext_fragment;
public static Myfragment newInstance() {
return new Myfragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myInflatedView = inflater.inflate(R.layout.Myfragment, container, false);
InitializeFragment(myInflatedView);
return myInflatedView;
}
private void InitializeFragment(View myInflatedView) {
edittext_fragment = (EditText) myInflatedView.findViewById(R.id.edittext_fragment_caption);
}
public void set_fragment_editext(String value) {
edittext_fragment.setText(value);
}
}
答案 0 :(得分:0)
在调用setText函数后调用oncreateview,因此为空指针。
将字符串作为参数传递。
public class Myfragment extends Fragment {
EditText edittext_fragment;
String textViewText;
public static Myfragment newInstance(String textViewText){
this.textViewText = textViewText;
return new Myfragment();
}
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myInflatedView = inflater.inflate(R.layout.Myfragment, container, false);
InitializeFragment(myInflatedView);
set_fragment_editext(textViewText);
return myInflatedView;
}
private void InitializeFragment(View myInflatedView) {
edittext_fragment = (EditText) myInflatedView.findViewById(R.id.edittext_fragment_caption);
}
private void set_fragment_editext(String value) {
edittext_fragment.setText(value);
}
}
活动代码:
Myfragment mFragment = Myfragment.newInstance("Hello World");
答案 1 :(得分:0)
1)通过片段生命周期需要一些时间。
2)它发生在后台。
Myfragment mFragment = new Myfragment();
fragmentTransaction.replace(R.id.fragment_container, mFragment);
fragmentTransaction.commit();
// EditText is not yet initialized
mFragment.set_fragment_editext("Hello world");//setting edit text in fragment throws exception
通常的android任务是,只有在某些组件被初始化时才应该做一些事情。
在您的情况下,您可以通过多种方式执行此操作:
1)推迟字符串集。
class Myfragment extedns Fragment {
private String mEditTextString;
private EditText edittext_fragment;
public void setEditText(String string) {
mEditTextString = string;
if (edittext_fragment != null) {
edittext_fragment.setText(string);
}
}
private void InitializeFragment(View myInflatedView) {
edittext_fragment = (EditText) myInflatedView.findViewById(R.id.edittext_fragment_caption);
if (mEditTextString != null) {
edittext_fragment.setText(mEditTextString);
}
}
}
2)在编辑文本初始化时通过回调活动。请查看本指南https://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity
3)正如@Karishnu Poddar所提到的,如果你想将编辑文本设置为一次就可以将其传递给片段参数