简体   繁体   中英

NullPointerException while passing data from activity to fragment via Bundle

Like several users I too got stuck in this thing. I try to pass data to a Fragment from an activity using bundle code I got from here & I'm stuck because My app crashes as soon as I select that fragment from navigation drawer. Can anyone point out the silly mistake I've committed ?

Here is the ACTIVITY CODE where I create bundle:

if(code==1)
            {
                Bundle bundle = new Bundle();
                bundle.putString("mailsession", mail);
                EditMyProfile fragobj = new EditMyProfile();
                fragobj.setArguments(bundle);
                Intent l = new Intent(this, MainActivity.class);
                startActivity(l);


                Toast.makeText(getBaseContext(), "Login Successfully",
                        Toast.LENGTH_SHORT).show();
            }

Following is the FRAGMENT CODE :

public class EditMyProfile extends Fragment {
Button savebutton;
    String emailofuser;

    EditText name,age,bloodgroup,city,state,phone,email;
    public EditMyProfile(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {



        View rootView = inflater.inflate(R.layout.fragment_edit_profile, container, false);

        emailofuser = this.getArguments().getString("mailsession");
        email=(EditText)rootView.findViewById(R.id.mailU);
        email.setText(emailofuser);
        name=(EditText)rootView.findViewById(R.id.nameU);
        age=(EditText)rootView.findViewById(R.id.ageU);
        bloodgroup=(EditText)rootView.findViewById(R.id.bgroupU);
        city=(EditText)rootView.findViewById(R.id.cityU);
        state=(EditText)rootView.findViewById(R.id.stateU);
        phone=(EditText)rootView.findViewById(R.id.mobU);



        savebutton=(Button)rootView.findViewById(R.id.Savbutton);


         savebutton.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 Intent intent = new Intent(getActivity(),Login.class);
                 startActivity(intent);
                 Toast.makeText(getActivity().getApplicationContext(),"AVC",Toast.LENGTH_LONG).show();
             }
         });



        return rootView;
    }
}

There is nothing happening with the fragment. So if I understand the problem, pass the bundle to the activity and do the setArgument in Mainactivity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM