I am trying to send a calculated result from my Activity to a Fragment.
I searched how to do it and I am trying to it with a Bundle. How to pass values between Fragments
I often had a NPE problem but back then I could fix them by myself I also already read: 'What is NPE and how can I fix that' but I do not know how to fix it in this specific case.
I am new to Coding sorry for any inconveniences & also sorry if I forgot any code but I thought those 2 classes would suffice.
Error:
'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
Here is the class with my Bundle.
KalorienZaehlerRechner.java: (FragmentActivity)
public class KalorienzaehlerRechner extends FragmentActivity {
Button btn;
EditText userProteine;
EditText userKH;
EditText userKHz;
EditText userFett;
EditText userGegG;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_kalorienzaehlerrechner);
userProteine = findViewById(R.id.userGegPRechner);
userKH = findViewById(R.id.userGegKHRechner);
userKHz = findViewById(R.id.userGegKHzRechner);
userFett = findViewById(R.id.userGegFRechner);
userGegG = findViewById(R.id.userGegG);
btn = findViewById(R.id.btnhin);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gerichthinzufuegen();
}
});
}
public void gerichthinzufuegen(){
int usG = Integer.parseInt(userGegG.getText().toString());
int usKH = Integer.parseInt(userKH.getText().toString());
int usKHz = Integer.parseInt(userKHz.getText().toString());
int usP = Integer.parseInt(userProteine.getText().toString());
int usF = Integer.parseInt(userFett.getText().toString());
int USg = usG/100;
float resultP = (usP * USg) * 4.1f;
float resultF = (usF * USg) * 9.1f;
float resultKH = (usKH * USg) * 4.1f;
float userKcal = resultP + resultF + resultKH;
int intUserKcal = Math.round(userKcal);
Bundle bundle = new Bundle();
bundle.putInt("data", intUserKcal);
Fragment fragmentobject = new Fragment();
fragmentobject.setArguments(bundle);
}
}
And here is my Fragment. I want to receive the result here to display it in a TextView.
Kalorienzähler.java:
public class KalorienzählerFragment extends Fragment {
Button neuesGericht;
TextView uKcal;
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View kalorienZaehler = inflater.inflate(R.layout.fragment_kalorienzaehler, container, false);
uKcal = kalorienZaehler.findViewById(R.id.userGegKcal);
neuesGericht = kalorienZaehler.findViewById(R.id.btnGericht);
String userKcal = getArguments().getString("data");
uKcal.setText(userKcal);
neuesGericht.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openZaehlerRechner();
}
});
return kalorienZaehler;
}
public void openZaehlerRechner(){
Intent ZaehlerRechner = new Intent(getActivity(), KalorienzaehlerRechner.class);
startActivity(ZaehlerRechner);
}
}
Thanks to everyone who will help!