【问题标题】:Failure to get fragment address from MainActivity when trying to update an edit text field尝试更新编辑文本字段时无法从 MainActivity 获取片段地址
【发布时间】: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


    【解决方案1】:

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

    尝试将 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();
    

    并在Fragment的onCreateView中使用如下获取标题

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

    How to pass a variable from Activity to Fragment, and pass it back?

    【讨论】:

    • 您好 QuantumTiger,非常感谢您的解决方案。你是绝对正确的,一旦我遵循“捆绑”解决方案,使用“getArguments()”,NPE 就消失了。但是我必须添加 getArguments()…。我的 Fragment 的 onActivityCreated(..) 方法的代码行,而不是你在 onCreateView() 方法起作用之前的建议,我不太明白。 (当我不明白为什么一段代码正在工作时,我总是有点不高兴,尽管它确实工作很好!)我也跟着你的链接到'......将变量从活动传递到片段......'
    • 这加强了您的解决方案,我倾向于同意那些说 developer.android.com 文档根本没有很好地解释这个领域的评论者!
    • 很高兴它有帮助。在片段、活动和视图寻呼机之间传递数据时,我也遇到过类似的困难......
    • 就 NPE 而言,这一切都是为了确保在创建视图之前不要访问视图中的元素,因此在 onCreateView 中读取包很好,但从您的经验来看很明显那时还没有创建编辑文本字段
    【解决方案2】:

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

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

    然后通过标签找到它:

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

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

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

    【讨论】:

    • Assaf 感谢您查看我的问题,-正如您从上面 QuantumTiger 的回答中看到的那样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-17
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多