I am completely new to Android and I have to submit this app to end my college degree, but I am having this problem where my fragment crashes my entire app.
To explain what I have done up until now is, I have a LoginActivity where I sent the userId through the Intent and have the id of the current user on my DashboardActivity (and I can display it), but in the DashboardActivity I have a bottom navigation bar that navigates to my FormFragment and my DataFragment.
Right now, want I would love to pass the userId value of the current user from the DashboardActivity to my DataFragment, so that I can display dynamically the user data according to the userId.
So with it, I found that the best option is to use bundle, but I don't now why (because I am completely new to this) my app crashes every time I switch from my FormFragment to my DataFragment.
Can you help me? I am desperate xD
This is my DashboardActivity code:
public class PainelActivity extends AppCompatActivity {
    private Button buttonLogout;
    private TextView textViewId;
    private Object DashboardFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_painel);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_dashboard)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);
        // gets the id from the Intent
        Intent get = getIntent();
        String userId = get.getStringExtra(LoginActivity.EXTRA_ID);
        // Send the id to the Fragments
        Bundle bundle = new Bundle();
        bundle.putString("userId", userId);
        Fragment fragment = new Fragment();
        fragment.setArguments(bundle);
        getSupportFragmentManager().beginTransaction()
                .add(R.id.nav_host_fragment, fragment).commit();
        // see the id on the screen
        textViewId = findViewById(R.id.textViewId);
        textViewId.setText(userId);
        // logout
        buttonLogout = findViewById(R.id.logoutButton);
        buttonLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openMainActivity();
            }
        });
    }
    public void openMainActivity() {
        Intent HomePage = new Intent(this, MainActivity.class);
        startActivity(HomePage);
        Toast.makeText(PainelActivity.this, "Terminou a sua sessão.",
                Toast.LENGTH_LONG).show();
    }
}
And this is my DataFragment code:
public class DashboardFragment extends Fragment {
    INodeJS myAPI;
    private TextView textViewResult;
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
        //Init API
        Retrofit retrofit = RetrofitClient.getInstance();
        myAPI = retrofit.create(INodeJS.class);
        textViewResult = root.findViewById(R.id.text_view_result);
        // gets the id from the activity
        if (getArguments() != null) {
            String userId = getArguments().getString("userId");
            int uid = Integer.parseInt(userId);
            Call<List<DataResult>> call = myAPI.executeGetData(uid);
            call.enqueue(new Callback<List<DataResult>>() {
                @Override
                public void onResponse(Call<List<DataResult>> call, Response<List<DataResult>> response) {
                    if (response.code() == 200) {
                        List<DataResult> DATA = response.body();
                        for (DataResult data: DATA) {
                            String content = "";
                            content += "Data: " +data.getDta() + "\n";
                            content += "Hora: " +data.getHora() + "\n";
                            content += "Glicémia: " +data.getIndiceGlicemia() + "\n";
                            content += "Insulina: " +data.getInsulina() + "\n";
                            content += "Medicação: " +data.getMedicacao() + "\n\n";
                            textViewResult.append(content);
                        }
                    }
                }
                @Override
                public void onFailure(Call<List<DataResult>> call, Throwable t) {
                    textViewResult.setText(t.getMessage());
                }
            });
        }
        return root;
    }
}
Thank you and have a nice day!
(I just edited my code and updated my question, right now, my app doesn't crash but I don't see the data.)
 
     
     
     
    