-2

I noticed that my app only when is closed by system during open there is nullpointexception. Look I have Activity here I get data form db and create an object. Then I create new Fragment and set before created object to frament by setter. And use fragment manager to display fragment. Any here is a problem because in onCreate method field with my object is null. I am sure I seted object and it wasnt null.

Activity where I get data from db and create Fragment

public class ActivityForSingle extends AppCompatActivity {
  Employee emloyee;

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_for_single);
    ...

    getEmployee(); // here I get emplyee from db and create employee object

    SitePersonFragment f = new SitePersonFragment();
            f.setEmpoyee(employee); // here emplyee is not null
            getSupportFragmentManager().beginTransaction().add(R.id.frameForFragment, f, "USER_SITE").commit();


}


public class SitePersonFragment extends Fragment implements Update, DatePickerDialog.OnDateSetListener {

 Employee employee;

  setEmployee(Employee e){
      this.emplyee = e;
  //here e and employee is not null
   }

  @Override
     public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

  //here employee is null
  // why??
 }

What happend? I cannot debug because app is destroyed.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
JavaMan
  • 87
  • 1
  • 5

2 Answers2

0
  @Override
     public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    //here employee is null
    // why??
 }

Because the view might be created before you set the Employee.

Additionally, getEmployee()... does that return something? emloyee is never assigned in the code you posted. Why wouldn't it be null?

Related: Best practice for instantiating a new Android Fragment (requires a Serializable or Parcelable class)

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

I changed 'pracownikFragment tof`

JavaMan
  • 87
  • 1
  • 5