简体   繁体   English

尝试更新编辑文本字段时无法从MainActivity获取片段地址

[英]Failure to get fragment address from MainActivity when trying to update an edit text field

I have a MainActivity (MA) running several fragments, which do not talk to each other but communicate to the MA via the 'onClick' android mechanism. 我有一个运行几个片段的MainActivity(MA),这些片段不会互相交谈,而是通过“ onClick” android机制与MA通信。 Driven by a spinner choice and an 'onClick' button press, we arrive in the RetrievePatientRecord method of the MA. 在微调器选择和“ onClick”按钮按下的驱动下,我们到达MA的RetrievePatientRecord方法。

I carry out a database call to get the chosen db record (as per the commented line) and then test to see if there is an existingPatient fragment present into which I intend to put the database record's various fields as its essentially a form. 我进行数据库调用以获取选定的db记录(按照注释行),然后进行测试以查看是否存在现存的Pation片段,我打算将数据库记录的各个字段作为其本质形式放入其中。 With a not found, 'null' condition, I instantiate a new instance of a ExistingPatientFg fragment. 在未找到“空”条件的情况下,我实例化了ExistingPatientFg片段的新实例。 However when I attempt to use the existingPatient reference via a setter in the ExistPatientFg fragment to set the 'Title' field value the program falls over with a NPE ! 但是,当我尝试通过ExistPatientFg片段中的设置器使用existingPatient引用来设置“标题”字段值时,程序将被NPE淘汰!

I have tried to 'get the fragment address' using commented out line … getFragmentManager().findFragmentById(R.id….) but interestingly using android studio's code completion facility the only R.id. 我试图使用注释行...“ getFragmentManager()。findFragmentById(R.id…。)”来“获取片段地址”,但有趣的是,使用android studio的代码完成工具使用了唯一的R.id。 object it will offer is the 'fragment_container' which though it compiles, it falls over on this line of code if I leave it in. 它会提供的对象是“ fragment_container”,尽管可以编译,但如果我保留它,它将落在这行代码上。

So I'm a bit stumped. 所以我有点困惑。 I've read through most of the other S/O answers in this fragment/MainActivity/edit text field, subject area but haven't been able to get a solution which can help me, so I'm after some assistance here. 我已经阅读了该片段/ MainActivity / edit文本字段,主题区域中的大多数其他S / O答案,但是还没有找到可以帮助我的解决方案,因此我在这里寻求帮助。 (also it may be relevant that my activity_main.xml layout file is using a FrameLayout layout with its id as 'fragment_container') (也可能与我的activity_main.xml布局文件使用ID为'fragment_container'的FrameLayout布局有关)

   public void RetrievePatientRecord(View view) {
//   do database query to get record before displaying in patient record fragment
             this.patientID = findPatientRecordFg.patientID;
        Log.d(DEBUGTAG, " ---->>>>> retrieve this record L 404 with ID =  " + findPatientRecordFg.patientID);

// db query for existing patient's records
          findPatientRecordFg.db.getPatientRecords(patientID);

// populate patientdetails fragment fields and re display patientdetails fragment.

        if (existingPatient != null) {

            populatePatientDetails_fgWithCurrentValues();
            trans = getFragmentManager().beginTransaction();
            trans.replace(R.id.fragment_container, existingPatient);
            trans.addToBackStack(null);
            trans.commit();
        }else
        if (existingPatient == null){

            existingPatient = new ExistingPatientFg();
          // ExistingPatientFg existingPatient = (ExistingPatientFg)getFragmentManager().findFragmentById(R.id.fragment_container);
            cTitle      = findPatientRecordFg.db.geteTitle();

            Log.d(DEBUGTAG, " ######## -->>>>> reached just before setTitleField L 424 with value Title  =  "+ cTitle);
            existingPatient.setTitleField(cTitle);

            //populatePatientDetails_fgWithCurrentValues();
            trans = getFragmentManager().beginTransaction();
            trans.replace(R.id.fragment_container, existingPatient);
            trans.addToBackStack(null);
            trans.commit();
        }
    }

the fragment's java code is :- 该片段的Java代码为:-

public class ExistingPatientFg extends Fragment {
public ButtonPlus btn;
public ButtonPlus analysisBtn;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.patientdetails_fg, container, false);
    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    EditText editText = (EditText)getView().findViewById(R.id.bannerText);
    editText.setText("Existing Patient");

    btn = (ButtonPlus)getActivity().findViewById(R.id.Goto_Scanning_Page);

    analysisBtn = (ButtonPlus)getView().findViewById(R.id.Analysis);
    btn.setEnabled(true);

}


public void setTitleField(String string){
    EditText et = (EditText)getView().findViewById(R.id.Title);
    et.setText(string);
}

You can either find the fragment by tag that you supply when adding the fragment: 您可以在添加片段时通过提供的标签找到片段:

getFragmentManager().beginTransaction().replace(R.id.fragment_container, existingPatient, "Your Tag Here");

and than find it by tag: 而不是通过标签找到它:

getFragmentManager().findFragmentByTag("Your Tag Here")

Or, if you still want to find by ID - it should be the one that is returned by: fragment.getID(); 或者,如果您仍然想按ID查找-应该是通过以下方式返回的ID:fragment.getID(); it should be either the container of the fragment or the top layout in the fragment I'm not sure. 它应该是片段的容器或我不确定的片段中的顶部布局。 (So you can check for debugging after adding the fragment (因此,您可以在添加片段后检查调试

if fragment.getID()==R.id.container

I suspect that the null pointer exception is not that you don't have a reference to the fragment, but that you are calling setTitleField before the fragment view has been inflated so the edit text field doesn't exist yet. 我怀疑空指针异常不是因为您没有对该片段的引用,而是在片段视图膨胀之前调用setTitleField,因此编辑文本字段尚不存在。

Try moving setTitleField to after the trans.commit or better yet pass the title to the fragment as an extra argument. 尝试将setTitleField移到trans.commit之后,或者最好将标题作为额外参数传递给片段。

existingPatient = new ExistingPatientFg();
Bundle bundle = new Bundle();
bundle.putString("title",cTitle);
existingPatient.setArguments(bundle);                 
trans = getFragmentManager().beginTransaction();
trans.replace(R.id.fragment_container, existingPatient);
trans.addToBackStack(null);
trans.commit();

And use the following inthe Fragment's onCreateView to get the title 并在Fragment的onCreateView中使用以下内容获取标题

String cTitle = getArguments().getString("title");

See How to pass a variable from Activity to Fragment, and pass it back? 请参阅如何将变量从Activity传递到Fragment,并将其传递回?

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

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