简体   繁体   English

设置来自活动的片段的编辑文本,使空指针

[英]Setting edit text of fragment from Activity giving null pointer

I have just started using fragments. 我刚刚开始使用片段。 I have created a edit text in fragment and I want to fill it from activity but it is giving me null pointer exception. 我已经在片段中创建了一个编辑文本,我想从活动中填充它,但这给了我空指针异常。 May be OnCreateView runs after fragmenttransaction is committed. 提交fragmenttransaction后,OnCreateView可能会运行。 I have made it working from another way but I am just a little curious why it is not working. 我已经从另一种方式使它起作用了,但是我只是有点好奇为什么它不起作用。 I have searched it but could not find answer of my problem. 我已经搜索过,但找不到我的问题的答案。 Any help would be appreciated 任何帮助,将不胜感激

Here is my code 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

Then in fragment 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 is called after you call the setText function, hence the null pointer. 调用setText函数后将调用oncreateview,因此将调用null指针。

Pass the string as an argument. 将字符串作为参数传递。

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 code: 活动代码:

Myfragment mFragment = Myfragment.newInstance("Hello World");

1) It takes some time to pass through fragment lifecycle. 1)通过片段生命周期需要一些时间。

2) It happens in the background. 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

It is common android task that you should do some things only when some components are inittialized. 这是常见的android任务,只有在初始化某些组件时才应该做一些事情。

In your case you can do this in several ways: 在您的情况下,您可以通过以下几种方式执行此操作:

1) Postpone string set. 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) By callback to activity when edit text is inititlized. 2)通过在初始化编辑文本时回调活动。 Please take a look at this guide https://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity 请查看此指南https://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

3) As mentioned by @Karishnu Poddar if you want to set edit text just once you can pass it to fragment arguments 3)如@Karishnu Poddar所述,如果您只想设置一次编辑文本,则可以将其传递给片段参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM