片段中间(I):从edittext获取输入,在片段的textview中设置文本

孤独的学习者

确实需要一些建议,不知道这里出了什么问题。

上下文:2个片段,每个片段都有一个Textview,主要活动有2个按钮和一个edittext

目的:在主活动的edittext框中键入hello,然后单击片段1的按钮,textview将变为hello。

问题:

在edittext中输入hello并单击按钮1时,遇到运行时错误。

Logcat:

E/AndroidRuntime(1291): FATAL EXCEPTION: main
E/AndroidRuntime(1291): android.view.InflateException: Binary XML file line #29: Error inflating class fragment
E/AndroidRuntime(1291): android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
E/AndroidRuntime(1291):android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
E/AndroidRuntime(1291):android.view.LayoutInflater.inflate(LayoutInflater.java:489)
E/AndroidRuntime(1291): android.view.LayoutInflater.inflate(LayoutInflater.java:396)
E/AndroidRuntime(1291): com.example.FragmentOne.onCreateView(FragmentOne.java:19)
E/AndroidRuntime(1291):android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)

fragment_one.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:background="#00ffff">

 <TextView
       android:id="@+id/textView1"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_weight="1"
       android:text="This is fragment No.1"
       android:textStyle="bold" />
 </LinearLayout>

activity_main.xml

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/easy"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10" >

            <requestFocus />
        </EditText>

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="selectFrag"
            android:text="Fragment No 1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="selectFrag"
            android:text="Fragment No 2" />

        <fragment
            android:name="com.example.FragmentTwo"
            android:id="@+id/fragment_place"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void selectFrag(View view) {
        Fragment fr;    
        if(view == findViewById(R.id.button2)) {
            fr = new FragmentTwo();

        }else {
            fr = new FragmentOne();
        }
        FragmentManager fm = getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(R.id.fragment_place, fr);
        fragmentTransaction.commit();

   }

}

FragmentOne.java

public class FragmentOne extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) 
    {
        View view = inflater.inflate(R.layout.fragment_one, container, false);
        TextView monthlypayment=  (TextView) view.findViewById(R.id.textView1);
        EditText easy = (EditText) inflater.inflate(R.layout.activity_main, container, false).findViewById(R.id.easy);
        monthlypayment.setText(easy.getText().toString());

        return view;
    }
}
拉贡南丹

您可以通过两种方式将片段添加到活动布局:

  1. 在活动的布局文件中声明该片段。

  2. 以编程方式将片段添加到现有ViewGroup。

两种方法都在文档中提到

http://developer.android.com/guide/components/fragments.html

如果要将片段添加到容器中,则需要使用xml中的ViewGroup。一般FrameLayout使用。所以在xml中有以下内容

 <FrameLayout
        android:id="@+id/fragment_place"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

活动代码很好。在“按钮”上单击,可以替换容器中的相应片段。

在您的片段中onCreateView,为片段布局充气,并使用view对象初始化该布局的视图。但是您会膨胀activity_main.xml,这不是必需的。

报价文件

具体来说,该片段可以使用getActivity()访问Activity实例,并轻松执行诸如在活动布局中查找视图之类的任务

所以EditText你可以使用

EditText easy = (EditText)getActivity().findViewById(R.id.easy);

或者单击进行初始化EditTextActivityButton获取值EditText,然后可以将EditTextfrom的值传递ActivityFragment

将数据从活动发送到Android中的片段

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Activity中的片段中设置TextView

来自分类Dev

在Activity中的片段中设置TextView

来自分类Dev

调整片段中TextView的文本大小

来自分类Dev

如何在片段中替换textView的文本

来自分类Dev

如何从片段中的OnListItemClick更改TextView的文本

来自分类Dev

调整片段中TextView的文本大小

来自分类Dev

片段中缺少 TextView 数据(文本)

来自分类Dev

Android:无法在片段中设置TextView值

来自分类Dev

从片段获取edittext值

来自分类Dev

如何从片段获取EditText

来自分类Dev

从viewpager中的片段获取edittext值

来自分类Dev

Android从片段中的AlertDialog获取EditText值

来自分类Dev

如何从xml片段中获取EditText值?

来自分类Dev

在Android中设置并获取使用标签片段

来自分类Dev

从片段中以编程方式添加的edittext中获取价值

来自分类Dev

尝试更改片段中的textView文本时发生NullPointerException

来自分类Dev

片段到片段 EditText

来自分类Dev

在片段中设置焦点

来自分类Dev

加载片段后,Android 在活动中获取 textView

来自分类Dev

设置片段的文本字段

来自分类Dev

如何从URL片段获取输入?

来自分类Dev

从片段中获取资源

来自分类Dev

在ViewPager片段中更新TextView

来自分类Dev

从片段访问布局中的TextView

来自分类Dev

Settext到TextView中的片段

来自分类Dev

更改片段中的 TextView 内容

来自分类Dev

如何在 Android 中更改设置片段的文本颜色?

来自分类Dev

从图库中获取图像以在imageview中设置片段?

来自分类Dev

在片段内设置 onCreate 的 edittext 值