I have two fragment with edittext, data from first edittext I gets but after I call second fragment I can't get data from edittext in second fragment. The Logcat said me that data from second edittext is void.
This is a first fragment class
package com.example.n;
import android.annotation.TargetApi;
import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class FragmentAdd extends Fragment {
EditText et1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.frag_add, container, false);
et1 = (EditText) rootView.findViewById(R.id.editText);
return rootView ;
}
}
This is a second fragment class
package com.example.n;
import android.annotation.TargetApi;
import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class FragmentAdd2 extends Fragment {
EditText et2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.frag_add2, container, false);
et2 = (EditText) rootView.findViewById(R.id.editText2);
return rootView ;
}
public String getEditText(){
/*FragmentAdd2 fragmentAdd3 = new FragmentAdd2();
String data = fragmentAdd3.et2.getText().toString();*/
String data = et2.getText().toString();
return data;
}
}
This is a part of main class
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void onClick(View view){
FragmentAdd fragmentAdd = new FragmentAdd();
String data = fragmentAdd.et1.getText().toString();
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void onClick(View view){
FragmentAdd2 fragmentAdd2 = new FragmentAdd2();
String data = fragmentAdd2.et2.getText().toString();
}
*LogCat*
java.lang.IllegalStateException: Could not execute method of the activity
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
The code above points to a different
Fragment, if you want to get Text from theFragmentyou need to point to theFragmentyou added to the Transaction, and that will get rid of theNPEto get yourFragmentyou can useTagandFragmentManagerSuppose when you added the Fragment you use
Transaction.add(int,Fragment,tag)you can now call your Fragment withFragmentManager.findFragmentByTag(tag)after that since you want yourEditTextyou can cast it to your preferredFragmentClass .ie.FragmentAdd2orFragmentAddthen you can retrieve your stuff.Hope it helps