Chris Chris - 1 year ago 47
Android Question

get EditText value from Fragment through onClick

I have created an acticity with multiple fragments. When inside a fragment a button (image) is clicked, the fragment is replaced with a new fragment. This works fine.
One of these images/buttons is the 'Add' button opening the addition page. In this fragment there are several EditTexts and a button 'Insert'.

When pressing the 'Insert'-button the Edittext values must be retrieved. This is where it goes wrong.

This is my code (I excluded what does not seem to be necessary):

public class MainActivity extends AppCompatActivity {
FragmentTransaction fragmentTransaction;
EditText etDistancesInsertDays;

protected void onCreate(Bundle savedInstanceState) {

etDistancesInsertDays = (EditText)findViewById(;

// On create display home page (HomeFragment)
Fragment frRecords = new RecordsFragment();
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(, frRecords);


public void openDistancesInsert(){
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(, new DistanceInsertFragment());

public void switchFragment(View view){
switch (view.getId()) {

// Trial 1
EditText etText = (EditText)findViewById(;
String strDays = etDistancesInsertDays.getText().toString();
// Caused by: java.lang.NullPointerException
Toast.makeText(MainActivity.this, "Days: "+strDays, Toast.LENGTH_LONG).show();

// Trial 2
Fragment frInsert;
frInsert = getSupportFragmentManager().findFragmentById(;
View frViewInsert;
frViewInsert = frInsert.getView();
// Caused by: java.lang.NullPointerException
EditText etText = (EditText)frViewInsert.findViewById(;
String strDays = etDistancesInsertDays.getText().toString();
Toast.makeText(MainActivity.this, "Days: "+strDays, Toast.LENGTH_LONG).show();



And my

public class DistanceInsertFragment extends Fragment {
EditText etDistancesInsertDays, etDistancesInsertMonths, etDistancesInsertYears;

public DistanceInsertFragment() {
// Required empty public constructor

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View frViewInsert = inflater.inflate(R.layout.fragment_distance_insert, container, false);

// Set current dat in text field:
Calendar c = Calendar.getInstance();
String strDayOfMonth = Integer.toString(c.get(Calendar.DAY_OF_MONTH));
String strMonthOfYear = Integer.toString(c.get(Calendar.MONTH)+1);
String strYear = Integer.toString(c.get(Calendar.YEAR));
etDistancesInsertDays = (EditText)frViewInsert.findViewById(;
etDistancesInsertMonths = (EditText)frViewInsert.findViewById(;
etDistancesInsertYears = (EditText)frViewInsert.findViewById(;

return frViewInsert;

Both trial 1 and 2 gave the error "Caused by: java.lang.NullPointerException". Though I'm not sure wat is meant with it..

Answer Source

Should the code not be:

EditText etText = (EditText)findViewById(;
String strDays = etText.getText().toString();

Here you are getting text from the EditText, in your code you are trying to retrieve from the id etDistancesInsertDays