Chris Chris - 4 months ago 15
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):
MainActicity.java:

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

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

etDistancesInsertDays = (EditText)findViewById(R.id.etDistancesInsertDays);

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


}


public void openDistancesInsert(){
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container, new DistanceInsertFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle(getString(R.string.frTitleDistanceInsert));
}


public void switchFragment(View view){
switch (view.getId()) {
case R.id.btnInsertDistance:

// Trial 1
EditText etText = (EditText)findViewById(R.id.etDistancesInsertDays);
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(R.id.frDistanceInsert);
View frViewInsert;
frViewInsert = frInsert.getView();
// Caused by: java.lang.NullPointerException
EditText etText = (EditText)frViewInsert.findViewById(R.id.etDistancesInsertDays);
String strDays = etDistancesInsertDays.getText().toString();
Toast.makeText(MainActivity.this, "Days: "+strDays, Toast.LENGTH_LONG).show();

break;
}
}

}


And my DistanceInsertFragment.java

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

public DistanceInsertFragment() {
// Required empty public constructor
}


@Override
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(R.id.etDistancesInsertDays);
etDistancesInsertMonths = (EditText)frViewInsert.findViewById(R.id.etDistancesInsertMonths);
etDistancesInsertYears = (EditText)frViewInsert.findViewById(R.id.etDistancesInsertYears);
etDistancesInsertDays.setText(strDayOfMonth);
etDistancesInsertMonths.setText(strMonthOfYear);
etDistancesInsertYears.setText(strYear);

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

Should the code not be:

EditText etText = (EditText)findViewById(R.id.etDistancesInsertDays);
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