Android 将值从片段获取到mainActivity

Android 将值从片段获取到mainActivity,android,android-fragments,Android,Android Fragments,我有一个主要的活动。它的布局包含一个fragmentContainer和一个actionBar。然后我有两个不同的片段,包含不同的编辑文本。它们都有ID。当用户单击actionBar中的按钮时,我用片段填充fragmentContainer。一切都很完美 现在,当我按下按钮(在actionBar的菜单中)时,这个应用程序应该会从两个片段中收集所有EditText的内容。但它不起作用。当我试图直接从活动访问EditText时,它崩溃了。我知道我应该在片段的代码中创建一个公共方法,该方法将返回我感兴

我有一个主要的活动。它的布局包含一个fragmentContainer和一个actionBar。然后我有两个不同的片段,包含不同的编辑文本。它们都有ID。当用户单击actionBar中的按钮时,我用片段填充fragmentContainer。一切都很完美

现在,当我按下按钮(在actionBar的菜单中)时,这个应用程序应该会从两个片段中收集所有EditText的内容。但它不起作用。当我试图直接从活动访问EditText时,它崩溃了。我知道我应该在片段的代码中创建一个公共方法,该方法将返回我感兴趣的值。但我似乎做错了什么,因为无法从main(TabActivity)访问该方法

这是我的密码:

public class TabActivity extends Activity {
    ActionBar.Tab tab_alocare, tab_merc;
    Fragment fragmentTab1 = new aloc_fragment();
    Fragment fragmentTab2 = new merc_fragment();

    @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab); // this layout contains the fragmentContainer
        ...// here I do some actionBar stuff
        }
...
}
其中一个片段的示例如下:

public class aloc_fragment extends Fragment {
    EditText mEditText;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_aloc, container, false);
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mEditText = (EditText) getView().findViewById(R.id.edaloc);
    }
    public String getMyText() {
        String rez = "";
        if (mEditText.getText()!=null) {
            rez = mEditText.getText().toString();
        }
        return rez;
    }
}
现在。。。如果在活动代码中,我希望使用findViewById访问edaloc EditText,则会导致应用程序崩溃,并出现nullException。然后我尝试访问片段的public方法,以便它返回我需要的值。但是无法从TabActivity访问“getMyText”方法

因此,此代码不起作用:

@Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.save:
          Fragment alocfragment = (Fragment) getFragmentManager().findFragmentByTag("aloc");
          String s = alocfragment.getMyText(); // this is not compiling at all
我显然做错了什么。 请告知


谢谢

您的代码中有很多错误,您既没有在适当的位置声明也没有初始化片段。另外,您试图在
onOptionsItemSelected
中创建另一个fragment实例,这是错误的,您需要使用相同的fragment实例来访问具有值的函数。我希望你们知道如何添加和膨胀碎片。请参阅我的注释和代码更改

试试这个-

public class TabActivity extends Activity {
    ActionBar.Tab tab_alocare, tab_merc;
    //Declare fragments here
    Fragment fragmentTab1;
    Fragment fragmentTab2;

    @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab); // this layout contains the fragmentContainer

        //Initialize here
         fragmentTab1 = new aloc_fragment();
         fragmentTab2 = new merc_fragment();
        //inflate/add fragments here to the activity


        ...// here do your actionBar stuff
        }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.save:
        //Now, use same instance of fragment to access your function
        String s = fragmentTab1.getMyText();
    }
}

public class aloc_fragment extends Fragment {
    EditText mEditText;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_aloc,
        container, false);
        //hold edittext here in the variable
        mEditText = (EditText) view.findViewById(R.id.edaloc);
        return view;
    }
    public String getMyText() {
        String rez = "";
        if (mEditText.getText()!=null) {
            rez = mEditText.getText().toString();
        }
        return rez;
    }
}
另外,必须通过碎片教程


mEditText=(EditText)getView().findViewById(R.id.edaloc);您已将此添加到onActivityCreated中。因此,当您调用getMyText()时,mEditText引用不可用,因为只有在创建片段时才会创建它。我将其添加到onCreateView中。仍然只有在创建片段时才会调用changeonCreateView()。在onoptionSelected()中,您只是获取引用,而不是创建片段!!。为EditText“mEditText”创建文本更改侦听器。当edittext失去焦点意味着用户已完成输入文本时,将触发此侦听器。然后在getMyText()方法中,jus返回“rez”的值,其内容将在MeditTextThank的文本更改侦听器中获取,使用TextChangeListener和GlobalVariables,我可以做我需要的事情。我不太确定这是一个好方法,但它似乎一直有效,直到无法从选项卡activity访问方法getMyText为止。即使我遵循您的代码,内部方法getMyText也无法从activity访问。它似乎也没有被我打破,因为其他一切都很好。你给我的所有教程链接(顺便说一句,很久以前读过)都描述了如何将内容发送到片段,没有一个描述了从片段中获取内容的方法。我真的认为,因为它没有在正式文件中描述,那么它可能是像馅饼一样简单的东西,不值得在文件中提及。请用一个空白项目自己尝试一下。您甚至可以得到我在这里尝试实现的工作示例()。这正是我正在做的,除了这里也没有关于从片段中获取内容的参考。但您可以将此作为起点,并尝试自己从片段中获取值。只需在项目中的每个片段中放置一个EditText,并尝试在单击菜单中获取它们的值。到目前为止,我看到的唯一解决方案是使用GlobalVariable类,并在类似onTextChange listener的事件上从片段类内设置值。因为我还没有在网上看到任何人提出这个解决方案,我倾向于认为这可能是个坏主意。。。但我不能肯定这一点。而且我也没有其他有效的解决方案。因此,如果您有更好的解决方案,请提供建议。