设置来自 Activity 的片段的编辑文本,给出空指针

Setting edit text of fragment from Activity giving null pointer

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

这是我的代码 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

然后在片段class中:

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 在调用 setText 函数后调用,因此是空指针。

将字符串作为参数传递。

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

Activity代码:

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) 当编辑文本被初始化时通过回调到 activity。请看一下这份指南https://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

3) 如@Karishnu Poddar 所述,如果你想设置一次编辑文本,你可以将它传递给片段参数