当前位置: 首页 > 知识库问答 >
问题:

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

谯阳伯
2023-03-14

我有一个运行几个片段的Mainactive(MA),它们不会相互交谈,而是通过“onClick”android机制与MA通信。由微调器选择和“onClick”按钮驱动,我们到达MA的Retrieve病人记录方法。

我执行一个数据库调用以获取所选的db记录(根据注释行),然后测试是否存在一个existingPatient片段,我打算将数据库记录的各个字段放入其中,因为它本质上是一个表单。在未找到“null”条件下,我实例化了ExistingPatientFg片段的一个新实例。然而,当我试图通过ExistPatientFg片段中的setter来使用existingPatient引用来设置“Title”字段值时,程序会出现NPE!

我试图使用注释掉的行“获取片段地址”...getFragmentManager().findFragmentById(R.id....),但有趣的是,使用Android Studio的代码完成工具是唯一 R.id。它将提供的对象是“fragment_container”,尽管它可以编译,但如果我将其留在这行代码中,它就会落在这行代码上。

所以我有点难住了。我已经通读了这个片段/主要活动/编辑文本字段,主题区域中的大多数其他S/O答案,但还没有找到可以帮助我的解决方案,所以我在这里寻求一些帮助。(我的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();
        }
    }

片段的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);
}

共有2个答案

慕嘉茂
2023-03-14

您可以通过添加片段时提供的标签来查找片段:

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

而不是通过标签找到它:

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

或者,如果您仍然想通过ID查找-它应该是由:fragment.getID()返回的;它应该是片段的容器或片段中的顶部布局,我不确定。(所以您可以在添加片段后检查调试

if fragment.getID()==R.id.container
戴凯歌
2023-03-14

我怀疑空指针异常不是您没有对片段的引用,而是您在片段视图膨胀之前调用setTitleField,因此编辑文本字段尚不存在。

尝试将setTitleField移动到trans之后。提交,或者更好地将标题作为额外参数传递给片段。

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

并在片段的onCreateView中使用以下代码来获取标题

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

请参阅如何将变量从活动传递到片段,并将其传回?

 类似资料:
  • 问题内容: 我的工作流程中有一些JSONObject,并且通过将它们写入json文件来存储相同的JSONObject。 我想要一种有效的方式来更新json文件, 仅 更新 需要的字段, 以及更新的JSONObjects实例的内容。 例如: 档案中我有 在记忆中我有 更新将像: 通常,我想更新每个键的内容较新(不同)的值。 实际上我的代码是: 问题答案: 只需执行以下操作即可:

  • 我想在用户在EditText中键入文本时将文本放入我的应用程序中的某个字符串中,并使用它来生动地在活动上显示它(在不同的视图中...) - 就像谷歌的实时/即时搜索工作一样...

  • 下面是: 这是我要用于向中添加项的方法

  • 问题内容: 在我的应用程序中,我想要一个带有文本字段的警报。单击“完成”后,我想将文本字段输入保存为字符串。单击“取消”后,我只想关闭警报。我创建了这样的警报: 警报如下所示: 我想知道如何从文本字段中获取文本,以及如何为“完成”按钮和“取消”按钮创建事件。 问题答案: 您可以使用UIAlertController而不是UIAlertView。 我已经使用UIAlertController实现和测

  • 这是我的片段的代码: 现在,第一次,编辑文本完美地工作,我可以使用它,可以在它上面写,任何东西。如果我改变了片段,然后回来,我不能什么都不做。如果我按下编辑文本,键盘不会出现。甚至更多,如果我不;更改片段后,不要关闭键盘,这样当我返回时,我会打开键盘,如果我按下编辑文本,然后在键盘上写,什么都不会出现。 这是我的 FragmentActivity,如果它有帮助的话: 编辑:它适用于4.x,但不适用

  • 如您所见,当按下按钮时,main activity运行“GetFromuser”函数。我想在该函数中获取edittext值。我该怎么做呢?