片段中间体(Ⅰ):从获取的EditText输入,在一个片段的TextView的设置文本 [英] Fragment Intermediate(I):Getting input from edittext, set text in textview of a fragment
问题描述
确实需要一些建议,不知道什么是错在这里。
上下文: 2片段,每一个TextView和主要活动有2个按钮和一个的EditText
目标: 在主要活动类型招呼到的EditText框, 当在按钮上单击片段1,TextView的将变更为你好。
问题:
面对运行时错误时输入招呼到的EditText,然后点击按钮1。
logcat的:
E / AndroidRuntime(1291):致命异常:主要
E / AndroidRuntime(1291):android.view.InflateException:二进制XML文件中的行#29:错误充气类片段
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版本=1.0编码=UTF-8&GT?;
<的LinearLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
机器人:后台=#00FFFF>
<的TextView
机器人:ID =@ + ID / textView1
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:layout_weight =1
机器人:文本=这是段1号
机器人:TEXTSTYLE =黑体/>
< / LinearLayout中>
activity_main.xml
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>
<的EditText
机器人:ID =@ + ID /易
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:EMS =10>
<不是requestFocus />
< /的EditText>
<按钮
机器人:ID =@ + ID /按钮1
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:的onClick =selectFrag
机器人:文本=片段1号/>
<按钮
机器人:ID =@ + ID /按钮2
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:的onClick =selectFrag
机器人:文本=片段2号/>
<片段
机器人:名称=com.example.FragmentTwo
机器人:ID =@ + ID / fragment_place
机器人:layout_width =match_parent
机器人:layout_height =match_parent/>
< / LinearLayout中>
MainActivity.java
公共类MainActivity延伸活动{
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
}
公共无效selectFrag(查看视图){
片段FR;
如果(查看== findViewById(R.id.button2)){
FR =新FragmentTwo();
}其他 {
FR =新FragmentOne();
}
FragmentManager FM = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place,法国);
fragmentTransaction.commit();
}
}
FragmentOne.java
公共类FragmentOne扩展片段{
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState)
{
查看查看= inflater.inflate(R.layout.fragment_one,集装箱,假);
TextView的monthlypayment =(TextView中)view.findViewById(R.id.textView1);
轻松的EditText =(EditText上)inflater.inflate(R.layout.activity_main,集装箱,假).findViewById(R.id.easy);
monthlypayment.setText(easy.getText()的toString());
返回查看;
}
}
有,你可以添加一个片段活动的布局方式有两种:
-
声明活动的布局文件中的片段。
-
编程片段添加到现有的ViewGroup。
这两种方法中提到的文档
http://developer.android.com/guide/components/fragments.html
如果你想片段添加到您需要使用的ViewGroup在XML容器中。一般的FrameLayout
被使用。因此,有下面的XML
<的FrameLayout
机器人:ID =@ + ID / fragment_place
机器人:layout_width =match_parent
机器人:layout_height =match_parent/>
该活动code是罚款。在按钮点击更换相应的片段在容器中。
在您的片段 onCreateView
你夸大的片段布局,并使用视图对象初始化视图布局。但是,你夸大 activity_main.xml
这是不需要的。
引用文档
具体而言,片段可以与访问活动实例 getActivity(),并轻松地执行,如发现在一个视图任务 活动布局
因此,对于的EditText
您可以使用
的EditText容易=(EditText上)getActivity()findViewById(R.id.easy)。
或初始化的EditText
在按钮
单击活动
得到的EditText
的值,那么你可以传递的的EditText
的值从活动
到片段
<一个href="http://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android">Send从活动数据在Android中以片段
really in need of some advice, dont know what is wrong here.
Context: 2 fragments with a Textview each and the main activity has 2 button and a edittext
Aim: Type hello into the edittext box in the main activity and When click on the button for fragment 1, the textview will change to hello.
Problem:
Face a runtime error when enter hello into edittext and click on button 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;
}
}
There are two ways you can add a fragment to the activity layout:
Declare the fragment inside the activity's layout file.
Programmatically add the fragment to an existing ViewGroup.
Both methods are mentioned in the docs
http://developer.android.com/guide/components/fragments.html
If you want add the fragment to a container you need to use a ViewGroup in xml. Generally FrameLayout
is used. So have the below in xml
<FrameLayout
android:id="@+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
The Activity code is fine. On Button click you replace the appropriate fragment in the container.
In your Fragment onCreateView
you inflate the fragment layout and use the view object to initialize views of that layout. But you inflate activity_main.xml
which is not required.
Quoting docs
Specifically, the fragment can access the Activity instance with getActivity() and easily perform tasks such as find a view in the activity layout
So for EditText
you can use
EditText easy = (EditText)getActivity().findViewById(R.id.easy);
Or Initialize EditText
in Activity
on Button
click get the value from EditText
and then you can pass the value of EditText
from Activity
to Fragment
Send data from activity to fragment in android
这篇关于片段中间体(Ⅰ):从获取的EditText输入,在一个片段的TextView的设置文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!