Obtendo o valor EditText de um fragmento na atividade principal
Coloquei três textos de edição em um fragmento e quero obter os valores do texto de edição no fragmento da atividade principal. Eu tentei o código a seguir, mas está jogandoNull Pointer Exception nogetName () método.
Classe de Fragmento
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;
}
}
Atividade principal
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();
}
});
}
}
Editar:
activity_main.xml
esta é minha principal atividade 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 esta é a exceção que recebo quando clico no botão
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)