设置活动给出空指针的片段的编辑文本

问题描述:

我刚开始使用片段。我在片段中创建了一个编辑文本,我想从活动中填充它,但它给了我空指针异常。可能是在片段事务提交后运行OnCreateView。我用另一种方式使它工作,但我只是有点好奇它为什么不起作用。我搜查了它,但找不到我的问题的答案。任何帮助,将不胜感激设置活动给出空指针的片段的编辑文本

这里是我的代码在活动:

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 

然后在片段类:您拨打的setText功能,因此空指针后

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); 
    } 

} 

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)它需要一些时间来穿过片段生命周期。

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波达提到的,如果你要设置编辑文本只有一次,你可以通过它进行分段参数

+0

非常感谢您的回答似乎是正确的一个 – SimpleMan

+0

@SimpleMan直截了当地说这个解决方案存在问题。 onAttachFragment在片段中的onViewCreated之前被调用。请稍等,我会修复我的解决方案:) –

+0

@SimpleMan我修复了我的答案。对不起,第一个与onAttachFragment() –