-2

enter image description here

I want to send data from LoginActivity to FragmentIndividual.
I've been thinking about it for two days, but I haven't solved it. I don't know why NullPointer happens. I'd really appreciate it if you could attach the code together.


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

    // Initialize Firebase Auth
    mAuth = FirebaseAuth.getInstance();

    FragmentManager fragmentManager = getSupportFragmentManager();
    final FragmentTransaction t = fragmentManager.beginTransaction();
    final FragmentIndividual fragmentIndividual = new FragmentIndividual();
    login_btn = (Button)findViewById(R.id.signIn);
    e1 = (EditText)findViewById(R.id.idText);
    login_btn.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            Bundle b2 = new Bundle();
            b2.putString("email", e1.getText().toString());
            fragmentIndividual.setArguments(b2);
            t.add(R.id.fram1234, fragmentIndividual);
            t.commit();
        }
    });

    TextView signup = (TextView) findViewById(R.id.signUp);
    signup.setOnClickListener(new TextView.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent signupIntent = new Intent(getApplicationContext(), SignupActivity.class);
            startActivity(signupIntent);
        }
    });

    findViewById(R.id.signIn).setOnClickListener(onClickLisener);
    findViewById(R.id.pwResetBtn).setOnClickListener(onClickLisener);
}

public class FragmentIndividual extends Fragment {
TextView t1;

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_individual, container, false);
    t1 = (TextView)v.findViewById(R.id.emailTv);
    Bundle b3 = getArguments();
    String email = b3.getString("email");
    t1.setText(email);

    return inflater.inflate(R.layout.fragment_individual, container, false);
}
}
Ruli
  • 2,592
  • 12
  • 30
  • 40
zyoon
  • 37
  • 2
  • 6
    Does this answer your question? [Send data from activity to fragment in Android](https://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android) – Ruli Oct 25 '20 at 07:08
  • You should return v in onCreateView, which was already inflated, instead of inflating again. – Hetfieldan24 Oct 25 '20 at 11:13
  • You could try moving this line `final FragmentTransaction t = fragmentManager.beginTransaction();` into the `onClick()` method for the login button. – David Wasser Oct 26 '20 at 12:01
  • Please don't post images of errors, post the errors as text. Also, are you sure this is the only place you create this Fragment? – Ryan M Oct 31 '20 at 09:58

1 Answers1

0

create method constructor in fragment example below :

public class ExampleFragment extends Fragment 
{
String  currency;
List<model> list ;
public ExampleFragment(List<model> list , String currency) {
    this.list = list;
    this.currency=currency;
}
}
  • This is terrible advice. This will only cause more crashes later, because `Fragment`s must have no-argument constructors. – Ryan M Oct 31 '20 at 09:56