通过Bundle将数据从活动传递到片段时发生NullPointerException

时间:2015-05-23 12:38:17

标签: android android-fragments

和几个用户一样,我也陷入了困境。我尝试使用从此处获得的捆绑代码将数据传递到活动的片段中。我被卡住了,因为我的应用程序一旦从导航抽屉中选择了该片段就会崩溃。谁能指出我所犯的愚蠢错误?

这是 ACTIVITY CODE ,我在其中创建了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();
            }

以下是 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;
    }
}

1 个答案:

答案 0 :(得分:0)

片段没有任何变化。因此,如果我理解了这个问题,请将bundle传递给activity并在Mainactivity中执行setArgument。