【发布时间】:2015-11-14 19:55:15
【问题描述】:
我有一个 MainActivity (MA) 运行几个片段,它们不相互通信,而是通过“onClick”android 机制与 MA 通信。 在微调器选择和“onClick”按钮按下的驱动下,我们到达 MA 的 RetrievePatientRecord 方法。
我执行数据库调用以获取所选的数据库记录(根据注释行),然后测试是否存在一个现有的患者片段,我打算将数据库记录的各个字段作为其本质上的一种形式放入其中. 在未找到“null”条件的情况下,我实例化了 ExistingPatientFg 片段的新实例。 但是,当我尝试通过 ExistPatientFg 片段中的设置器使用现有Patient 引用来设置“标题”字段值时,程序会因 NPE 而失败!
我尝试使用注释掉的行来“获取片段地址”... getFragmentManager().findFragmentById(R.id....) 但有趣的是使用 android studio 的代码完成工具是唯一的 R.id。它将提供的对象是“fragment_container”,虽然它可以编译,但如果我把它留在这行代码上,它就会落在这行代码上。
所以我有点难过。我已经阅读了这个片段/MainActivity/编辑文本字段、主题区域中的大多数其他 S/O 答案,但无法获得可以帮助我的解决方案,所以我在这里寻求一些帮助。 (我的 activity_main.xml 布局文件使用 FrameLayout 布局,其 id 为 'fragment_container' 也可能是相关的)
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();
}
}
片段的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);
}
【问题讨论】:
标签: java android android-fragments reference android-edittext