从主活动中的片段获取EditText值 [英] Getting EditText Value from a Fragment in main Activity
问题描述
我已经在片段中放置了三个编辑文本,并希望从主活动中获取片段中的编辑文本的值.我尝试了以下代码,但在 getName()方法中抛出了 nullpointerexception .
I have placed three edit text in a fragment and want to get the values of the edit text in the fragment from the main activity. I tried the following code but its throwing nullpointerexception in getName() method.
片段类
public class Fragment1 extends Fragment{
EditText name, age, gender;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag1, container);
name = (EditText) view.findViewById(R.id.name);
age = (EditText) view.findViewById(R.id.age);
gender = (EditText) view.findViewById(R.id.gender);
return view;
}
public String getName(){
String name1 = name.getText().toString().trim();
return name1;
}
public String getAge(){
String age1 = age.getText().toString().trim();
return age1;
}
public String getGender(){
String gender1 = gender.getText().toString().trim();
return gender1;
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
Fragment1 f1;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button);
f1 = new Fragment1();
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, f1.getName() +" "+ f1.getAge() +" "+ f1.getGender(), Toast.LENGTH_SHORT).show();
}
});
}
}
activity_main.xml
这是我的主要活动ui
this is my main activity ui
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ganesh.fragtask.MainActivity">
<fragment
android:id="@+id/fragment"
android:name="com.example.ganesh.fragtask.Fragment1"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp"
android:text="Button" />
</RelativeLayout>
Logcat 这是我单击按钮时得到的例外情况
Logcat this is the exception i get when i click the button
FATAL EXCEPTION: main
Process: com.example.ganesh.fragtask, PID: 6015
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.ganesh.fragtask.Fragment1.getName(Fragment1.java:33)
at com.example.ganesh.fragtask.MainActivity$1.onClick(MainActivity.java:25)
at android.view.View.performClick(View.java:5611)
at android.view.View$PerformClick.run(View.java:22276)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6195)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
推荐答案
f1 = new Fragment()
这不是您添加片段的方式.您只是在这里创建一个新的片段实例.片段的onCreateView永远不会被调用,因为片段尚未添加.因此,editText变量为null,您将获得异常.请研究一些片段.
This is not how you add fragments. You are just creating a new fragment instance here. The fragment's onCreateView never gets called because the fragment has not been added. Due to this, the editText variable is null and you get the exception. Please study fragments a little bit.
http://www.vogella.com/tutorials/AndroidFragments/article.html
这是添加片段的方式:
getSupportFragmentManager().beginTransaction().add(R.id.frag_layout_id, f1, "fragment_identifier").commit();
编辑
由于问题已使用xml进行了编辑,因此尽管您已使用XML声明了该片段,但Java中的f1实例并未对此进行引用.要获取通过xml添加的实例的句柄,请使用此
Since the question has been edited with the xml, although you have declared the fragment in XML, the f1 instance in your java does not refer to that. To get a handle for the instance added through xml, use this
Fragment_Example f1 = (Fragment_Example)getSupportFragmentManager().findFragmentById(R.id.fragment);
这篇关于从主活动中的片段获取EditText值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!