0

I am stuck with an NPE.

I am trying to access/get the value of an Edittext from a child fragment unfortunately it returns an NPE.

here is the the Child Fragment (MoincomeexpFragment) :

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_moincomeexp, container, false);

appSalary = view.findViewById(R.id.appSalary_edit_text);

return view;
    }

and this is for the Parent Activity:

String AppSalary;

AppSalary = MoincomeexpFragment.appSalary.getText().toString()
Cremlic
  • 88
  • 9

2 Answers2

0

Put this code in your parent activity.

EditText appSalary = findViewById(R.id.appSalary_edit_text);
String appSalaryText = appSalary.getText().toString()
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Dmarp
  • 208
  • 1
  • 3
  • 13
-1

Create singleton class

public class Currentvalue {

private String name;

public static Currentvalue getInstance() {
    return new Currentvalue();
}

public void setName(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

}

in Fragement-----

Currentvalue .getInstance().setName(appSalary.getText().toString)

in activity-------

Currentvalue currentvalue;

on create()

currentvalue = Currentvalue.getInstance();

String name=currentvalue.getName();

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • This is an anti-pattern, and not the proper way to define a singleton. Might as well use SharedPreferences – OneCricketeer Nov 28 '19 at 06:53
  • how can you say that This is an anti-pattern,and Mr. cricket_007 This doesn't really your answer the question asked plz read once again mr cricket_007 "(Parent Activity) Getting the EditText value from (Child Fragment) resulted an NPE " – paresh mandaliya Nov 28 '19 at 07:07
  • It's not "my question". In general, singletons in Android should be avoided in favor of sqlite, SharedPreferences, and other native persistence features – OneCricketeer Nov 28 '19 at 07:17